drm_crtc.c 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892
  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/list.h>
  33. #include <linux/slab.h>
  34. #include <linux/export.h>
  35. #include "drm.h"
  36. #include "drmP.h"
  37. #include "drm_crtc.h"
  38. #include "drm_edid.h"
  39. #include "drm_fourcc.h"
  40. /* Avoid boilerplate. I'm tired of typing. */
  41. #define DRM_ENUM_NAME_FN(fnname, list) \
  42. char *fnname(int val) \
  43. { \
  44. int i; \
  45. for (i = 0; i < ARRAY_SIZE(list); i++) { \
  46. if (list[i].type == val) \
  47. return list[i].name; \
  48. } \
  49. return "(unknown)"; \
  50. }
  51. /*
  52. * Global properties
  53. */
  54. static struct drm_prop_enum_list drm_dpms_enum_list[] =
  55. { { DRM_MODE_DPMS_ON, "On" },
  56. { DRM_MODE_DPMS_STANDBY, "Standby" },
  57. { DRM_MODE_DPMS_SUSPEND, "Suspend" },
  58. { DRM_MODE_DPMS_OFF, "Off" }
  59. };
  60. DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
  61. /*
  62. * Optional properties
  63. */
  64. static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
  65. {
  66. { DRM_MODE_SCALE_NONE, "None" },
  67. { DRM_MODE_SCALE_FULLSCREEN, "Full" },
  68. { DRM_MODE_SCALE_CENTER, "Center" },
  69. { DRM_MODE_SCALE_ASPECT, "Full aspect" },
  70. };
  71. static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
  72. {
  73. { DRM_MODE_DITHERING_OFF, "Off" },
  74. { DRM_MODE_DITHERING_ON, "On" },
  75. { DRM_MODE_DITHERING_AUTO, "Automatic" },
  76. };
  77. /*
  78. * Non-global properties, but "required" for certain connectors.
  79. */
  80. static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
  81. {
  82. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  83. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  84. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  85. };
  86. DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
  87. static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
  88. {
  89. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  90. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  91. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  92. };
  93. DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
  94. drm_dvi_i_subconnector_enum_list)
  95. static struct drm_prop_enum_list drm_tv_select_enum_list[] =
  96. {
  97. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  98. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  99. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  100. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  101. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  102. };
  103. DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
  104. static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
  105. {
  106. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  107. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  108. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  109. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  110. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  111. };
  112. DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
  113. drm_tv_subconnector_enum_list)
  114. static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
  115. { DRM_MODE_DIRTY_OFF, "Off" },
  116. { DRM_MODE_DIRTY_ON, "On" },
  117. { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
  118. };
  119. DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
  120. drm_dirty_info_enum_list)
  121. struct drm_conn_prop_enum_list {
  122. int type;
  123. char *name;
  124. int count;
  125. };
  126. /*
  127. * Connector and encoder types.
  128. */
  129. static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
  130. { { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
  131. { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
  132. { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
  133. { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
  134. { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
  135. { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
  136. { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
  137. { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
  138. { DRM_MODE_CONNECTOR_Component, "Component", 0 },
  139. { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
  140. { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
  141. { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
  142. { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
  143. { DRM_MODE_CONNECTOR_TV, "TV", 0 },
  144. { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
  145. { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
  146. };
  147. static struct drm_prop_enum_list drm_encoder_enum_list[] =
  148. { { DRM_MODE_ENCODER_NONE, "None" },
  149. { DRM_MODE_ENCODER_DAC, "DAC" },
  150. { DRM_MODE_ENCODER_TMDS, "TMDS" },
  151. { DRM_MODE_ENCODER_LVDS, "LVDS" },
  152. { DRM_MODE_ENCODER_TVDAC, "TV" },
  153. { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
  154. };
  155. char *drm_get_encoder_name(struct drm_encoder *encoder)
  156. {
  157. static char buf[32];
  158. snprintf(buf, 32, "%s-%d",
  159. drm_encoder_enum_list[encoder->encoder_type].name,
  160. encoder->base.id);
  161. return buf;
  162. }
  163. EXPORT_SYMBOL(drm_get_encoder_name);
  164. char *drm_get_connector_name(struct drm_connector *connector)
  165. {
  166. static char buf[32];
  167. snprintf(buf, 32, "%s-%d",
  168. drm_connector_enum_list[connector->connector_type].name,
  169. connector->connector_type_id);
  170. return buf;
  171. }
  172. EXPORT_SYMBOL(drm_get_connector_name);
  173. char *drm_get_connector_status_name(enum drm_connector_status status)
  174. {
  175. if (status == connector_status_connected)
  176. return "connected";
  177. else if (status == connector_status_disconnected)
  178. return "disconnected";
  179. else
  180. return "unknown";
  181. }
  182. /**
  183. * drm_mode_object_get - allocate a new identifier
  184. * @dev: DRM device
  185. * @ptr: object pointer, used to generate unique ID
  186. * @type: object type
  187. *
  188. * LOCKING:
  189. *
  190. * Create a unique identifier based on @ptr in @dev's identifier space. Used
  191. * for tracking modes, CRTCs and connectors.
  192. *
  193. * RETURNS:
  194. * New unique (relative to other objects in @dev) integer identifier for the
  195. * object.
  196. */
  197. static int drm_mode_object_get(struct drm_device *dev,
  198. struct drm_mode_object *obj, uint32_t obj_type)
  199. {
  200. int new_id = 0;
  201. int ret;
  202. again:
  203. if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
  204. DRM_ERROR("Ran out memory getting a mode number\n");
  205. return -ENOMEM;
  206. }
  207. mutex_lock(&dev->mode_config.idr_mutex);
  208. ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
  209. mutex_unlock(&dev->mode_config.idr_mutex);
  210. if (ret == -EAGAIN)
  211. goto again;
  212. else if (ret)
  213. return ret;
  214. obj->id = new_id;
  215. obj->type = obj_type;
  216. return 0;
  217. }
  218. /**
  219. * drm_mode_object_put - free an identifer
  220. * @dev: DRM device
  221. * @id: ID to free
  222. *
  223. * LOCKING:
  224. * Caller must hold DRM mode_config lock.
  225. *
  226. * Free @id from @dev's unique identifier pool.
  227. */
  228. static void drm_mode_object_put(struct drm_device *dev,
  229. struct drm_mode_object *object)
  230. {
  231. mutex_lock(&dev->mode_config.idr_mutex);
  232. idr_remove(&dev->mode_config.crtc_idr, object->id);
  233. mutex_unlock(&dev->mode_config.idr_mutex);
  234. }
  235. struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
  236. uint32_t id, uint32_t type)
  237. {
  238. struct drm_mode_object *obj = NULL;
  239. mutex_lock(&dev->mode_config.idr_mutex);
  240. obj = idr_find(&dev->mode_config.crtc_idr, id);
  241. if (!obj || (obj->type != type) || (obj->id != id))
  242. obj = NULL;
  243. mutex_unlock(&dev->mode_config.idr_mutex);
  244. return obj;
  245. }
  246. EXPORT_SYMBOL(drm_mode_object_find);
  247. /**
  248. * drm_framebuffer_init - initialize a framebuffer
  249. * @dev: DRM device
  250. *
  251. * LOCKING:
  252. * Caller must hold mode config lock.
  253. *
  254. * Allocates an ID for the framebuffer's parent mode object, sets its mode
  255. * functions & device file and adds it to the master fd list.
  256. *
  257. * RETURNS:
  258. * Zero on success, error code on failure.
  259. */
  260. int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
  261. const struct drm_framebuffer_funcs *funcs)
  262. {
  263. int ret;
  264. kref_init(&fb->refcount);
  265. ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
  266. if (ret)
  267. return ret;
  268. fb->dev = dev;
  269. fb->funcs = funcs;
  270. dev->mode_config.num_fb++;
  271. list_add(&fb->head, &dev->mode_config.fb_list);
  272. return 0;
  273. }
  274. EXPORT_SYMBOL(drm_framebuffer_init);
  275. static void drm_framebuffer_free(struct kref *kref)
  276. {
  277. struct drm_framebuffer *fb =
  278. container_of(kref, struct drm_framebuffer, refcount);
  279. fb->funcs->destroy(fb);
  280. }
  281. /**
  282. * drm_framebuffer_unreference - unref a framebuffer
  283. *
  284. * LOCKING:
  285. * Caller must hold mode config lock.
  286. */
  287. void drm_framebuffer_unreference(struct drm_framebuffer *fb)
  288. {
  289. struct drm_device *dev = fb->dev;
  290. DRM_DEBUG("FB ID: %d\n", fb->base.id);
  291. WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
  292. kref_put(&fb->refcount, drm_framebuffer_free);
  293. }
  294. EXPORT_SYMBOL(drm_framebuffer_unreference);
  295. /**
  296. * drm_framebuffer_reference - incr the fb refcnt
  297. */
  298. void drm_framebuffer_reference(struct drm_framebuffer *fb)
  299. {
  300. DRM_DEBUG("FB ID: %d\n", fb->base.id);
  301. kref_get(&fb->refcount);
  302. }
  303. EXPORT_SYMBOL(drm_framebuffer_reference);
  304. /**
  305. * drm_framebuffer_cleanup - remove a framebuffer object
  306. * @fb: framebuffer to remove
  307. *
  308. * LOCKING:
  309. * Caller must hold mode config lock.
  310. *
  311. * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
  312. * it, setting it to NULL.
  313. */
  314. void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
  315. {
  316. struct drm_device *dev = fb->dev;
  317. /*
  318. * This could be moved to drm_framebuffer_remove(), but for
  319. * debugging is nice to keep around the list of fb's that are
  320. * no longer associated w/ a drm_file but are not unreferenced
  321. * yet. (i915 and omapdrm have debugfs files which will show
  322. * this.)
  323. */
  324. drm_mode_object_put(dev, &fb->base);
  325. list_del(&fb->head);
  326. dev->mode_config.num_fb--;
  327. }
  328. EXPORT_SYMBOL(drm_framebuffer_cleanup);
  329. /**
  330. * drm_framebuffer_remove - remove and unreference a framebuffer object
  331. * @fb: framebuffer to remove
  332. *
  333. * LOCKING:
  334. * Caller must hold mode config lock.
  335. *
  336. * Scans all the CRTCs and planes in @dev's mode_config. If they're
  337. * using @fb, removes it, setting it to NULL.
  338. */
  339. void drm_framebuffer_remove(struct drm_framebuffer *fb)
  340. {
  341. struct drm_device *dev = fb->dev;
  342. struct drm_crtc *crtc;
  343. struct drm_plane *plane;
  344. struct drm_mode_set set;
  345. int ret;
  346. /* remove from any CRTC */
  347. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  348. if (crtc->fb == fb) {
  349. /* should turn off the crtc */
  350. memset(&set, 0, sizeof(struct drm_mode_set));
  351. set.crtc = crtc;
  352. set.fb = NULL;
  353. ret = crtc->funcs->set_config(&set);
  354. if (ret)
  355. DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
  356. }
  357. }
  358. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  359. if (plane->fb == fb) {
  360. /* should turn off the crtc */
  361. ret = plane->funcs->disable_plane(plane);
  362. if (ret)
  363. DRM_ERROR("failed to disable plane with busy fb\n");
  364. /* disconnect the plane from the fb and crtc: */
  365. plane->fb = NULL;
  366. plane->crtc = NULL;
  367. }
  368. }
  369. list_del(&fb->filp_head);
  370. drm_framebuffer_unreference(fb);
  371. }
  372. EXPORT_SYMBOL(drm_framebuffer_remove);
  373. /**
  374. * drm_crtc_init - Initialise a new CRTC object
  375. * @dev: DRM device
  376. * @crtc: CRTC object to init
  377. * @funcs: callbacks for the new CRTC
  378. *
  379. * LOCKING:
  380. * Takes mode_config lock.
  381. *
  382. * Inits a new object created as base part of an driver crtc object.
  383. *
  384. * RETURNS:
  385. * Zero on success, error code on failure.
  386. */
  387. int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
  388. const struct drm_crtc_funcs *funcs)
  389. {
  390. int ret;
  391. crtc->dev = dev;
  392. crtc->funcs = funcs;
  393. mutex_lock(&dev->mode_config.mutex);
  394. ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
  395. if (ret)
  396. goto out;
  397. crtc->base.properties = &crtc->properties;
  398. list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
  399. dev->mode_config.num_crtc++;
  400. out:
  401. mutex_unlock(&dev->mode_config.mutex);
  402. return ret;
  403. }
  404. EXPORT_SYMBOL(drm_crtc_init);
  405. /**
  406. * drm_crtc_cleanup - Cleans up the core crtc usage.
  407. * @crtc: CRTC to cleanup
  408. *
  409. * LOCKING:
  410. * Caller must hold mode config lock.
  411. *
  412. * Cleanup @crtc. Removes from drm modesetting space
  413. * does NOT free object, caller does that.
  414. */
  415. void drm_crtc_cleanup(struct drm_crtc *crtc)
  416. {
  417. struct drm_device *dev = crtc->dev;
  418. if (crtc->gamma_store) {
  419. kfree(crtc->gamma_store);
  420. crtc->gamma_store = NULL;
  421. }
  422. drm_mode_object_put(dev, &crtc->base);
  423. list_del(&crtc->head);
  424. dev->mode_config.num_crtc--;
  425. }
  426. EXPORT_SYMBOL(drm_crtc_cleanup);
  427. /**
  428. * drm_mode_probed_add - add a mode to a connector's probed mode list
  429. * @connector: connector the new mode
  430. * @mode: mode data
  431. *
  432. * LOCKING:
  433. * Caller must hold mode config lock.
  434. *
  435. * Add @mode to @connector's mode list for later use.
  436. */
  437. void drm_mode_probed_add(struct drm_connector *connector,
  438. struct drm_display_mode *mode)
  439. {
  440. list_add(&mode->head, &connector->probed_modes);
  441. }
  442. EXPORT_SYMBOL(drm_mode_probed_add);
  443. /**
  444. * drm_mode_remove - remove and free a mode
  445. * @connector: connector list to modify
  446. * @mode: mode to remove
  447. *
  448. * LOCKING:
  449. * Caller must hold mode config lock.
  450. *
  451. * Remove @mode from @connector's mode list, then free it.
  452. */
  453. void drm_mode_remove(struct drm_connector *connector,
  454. struct drm_display_mode *mode)
  455. {
  456. list_del(&mode->head);
  457. drm_mode_destroy(connector->dev, mode);
  458. }
  459. EXPORT_SYMBOL(drm_mode_remove);
  460. /**
  461. * drm_connector_init - Init a preallocated connector
  462. * @dev: DRM device
  463. * @connector: the connector to init
  464. * @funcs: callbacks for this connector
  465. * @name: user visible name of the connector
  466. *
  467. * LOCKING:
  468. * Takes mode config lock.
  469. *
  470. * Initialises a preallocated connector. Connectors should be
  471. * subclassed as part of driver connector objects.
  472. *
  473. * RETURNS:
  474. * Zero on success, error code on failure.
  475. */
  476. int drm_connector_init(struct drm_device *dev,
  477. struct drm_connector *connector,
  478. const struct drm_connector_funcs *funcs,
  479. int connector_type)
  480. {
  481. int ret;
  482. mutex_lock(&dev->mode_config.mutex);
  483. ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
  484. if (ret)
  485. goto out;
  486. connector->base.properties = &connector->properties;
  487. connector->dev = dev;
  488. connector->funcs = funcs;
  489. connector->connector_type = connector_type;
  490. connector->connector_type_id =
  491. ++drm_connector_enum_list[connector_type].count; /* TODO */
  492. INIT_LIST_HEAD(&connector->user_modes);
  493. INIT_LIST_HEAD(&connector->probed_modes);
  494. INIT_LIST_HEAD(&connector->modes);
  495. connector->edid_blob_ptr = NULL;
  496. list_add_tail(&connector->head, &dev->mode_config.connector_list);
  497. dev->mode_config.num_connector++;
  498. if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
  499. drm_connector_attach_property(connector,
  500. dev->mode_config.edid_property,
  501. 0);
  502. drm_connector_attach_property(connector,
  503. dev->mode_config.dpms_property, 0);
  504. out:
  505. mutex_unlock(&dev->mode_config.mutex);
  506. return ret;
  507. }
  508. EXPORT_SYMBOL(drm_connector_init);
  509. /**
  510. * drm_connector_cleanup - cleans up an initialised connector
  511. * @connector: connector to cleanup
  512. *
  513. * LOCKING:
  514. * Takes mode config lock.
  515. *
  516. * Cleans up the connector but doesn't free the object.
  517. */
  518. void drm_connector_cleanup(struct drm_connector *connector)
  519. {
  520. struct drm_device *dev = connector->dev;
  521. struct drm_display_mode *mode, *t;
  522. list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
  523. drm_mode_remove(connector, mode);
  524. list_for_each_entry_safe(mode, t, &connector->modes, head)
  525. drm_mode_remove(connector, mode);
  526. list_for_each_entry_safe(mode, t, &connector->user_modes, head)
  527. drm_mode_remove(connector, mode);
  528. mutex_lock(&dev->mode_config.mutex);
  529. drm_mode_object_put(dev, &connector->base);
  530. list_del(&connector->head);
  531. dev->mode_config.num_connector--;
  532. mutex_unlock(&dev->mode_config.mutex);
  533. }
  534. EXPORT_SYMBOL(drm_connector_cleanup);
  535. void drm_connector_unplug_all(struct drm_device *dev)
  536. {
  537. struct drm_connector *connector;
  538. /* taking the mode config mutex ends up in a clash with sysfs */
  539. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  540. drm_sysfs_connector_remove(connector);
  541. }
  542. EXPORT_SYMBOL(drm_connector_unplug_all);
  543. int drm_encoder_init(struct drm_device *dev,
  544. struct drm_encoder *encoder,
  545. const struct drm_encoder_funcs *funcs,
  546. int encoder_type)
  547. {
  548. int ret;
  549. mutex_lock(&dev->mode_config.mutex);
  550. ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
  551. if (ret)
  552. goto out;
  553. encoder->dev = dev;
  554. encoder->encoder_type = encoder_type;
  555. encoder->funcs = funcs;
  556. list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
  557. dev->mode_config.num_encoder++;
  558. out:
  559. mutex_unlock(&dev->mode_config.mutex);
  560. return ret;
  561. }
  562. EXPORT_SYMBOL(drm_encoder_init);
  563. void drm_encoder_cleanup(struct drm_encoder *encoder)
  564. {
  565. struct drm_device *dev = encoder->dev;
  566. mutex_lock(&dev->mode_config.mutex);
  567. drm_mode_object_put(dev, &encoder->base);
  568. list_del(&encoder->head);
  569. dev->mode_config.num_encoder--;
  570. mutex_unlock(&dev->mode_config.mutex);
  571. }
  572. EXPORT_SYMBOL(drm_encoder_cleanup);
  573. int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
  574. unsigned long possible_crtcs,
  575. const struct drm_plane_funcs *funcs,
  576. const uint32_t *formats, uint32_t format_count,
  577. bool priv)
  578. {
  579. int ret;
  580. mutex_lock(&dev->mode_config.mutex);
  581. ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
  582. if (ret)
  583. goto out;
  584. plane->base.properties = &plane->properties;
  585. plane->dev = dev;
  586. plane->funcs = funcs;
  587. plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
  588. GFP_KERNEL);
  589. if (!plane->format_types) {
  590. DRM_DEBUG_KMS("out of memory when allocating plane\n");
  591. drm_mode_object_put(dev, &plane->base);
  592. ret = -ENOMEM;
  593. goto out;
  594. }
  595. memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
  596. plane->format_count = format_count;
  597. plane->possible_crtcs = possible_crtcs;
  598. /* private planes are not exposed to userspace, but depending on
  599. * display hardware, might be convenient to allow sharing programming
  600. * for the scanout engine with the crtc implementation.
  601. */
  602. if (!priv) {
  603. list_add_tail(&plane->head, &dev->mode_config.plane_list);
  604. dev->mode_config.num_plane++;
  605. } else {
  606. INIT_LIST_HEAD(&plane->head);
  607. }
  608. out:
  609. mutex_unlock(&dev->mode_config.mutex);
  610. return ret;
  611. }
  612. EXPORT_SYMBOL(drm_plane_init);
  613. void drm_plane_cleanup(struct drm_plane *plane)
  614. {
  615. struct drm_device *dev = plane->dev;
  616. mutex_lock(&dev->mode_config.mutex);
  617. kfree(plane->format_types);
  618. drm_mode_object_put(dev, &plane->base);
  619. /* if not added to a list, it must be a private plane */
  620. if (!list_empty(&plane->head)) {
  621. list_del(&plane->head);
  622. dev->mode_config.num_plane--;
  623. }
  624. mutex_unlock(&dev->mode_config.mutex);
  625. }
  626. EXPORT_SYMBOL(drm_plane_cleanup);
  627. /**
  628. * drm_mode_create - create a new display mode
  629. * @dev: DRM device
  630. *
  631. * LOCKING:
  632. * Caller must hold DRM mode_config lock.
  633. *
  634. * Create a new drm_display_mode, give it an ID, and return it.
  635. *
  636. * RETURNS:
  637. * Pointer to new mode on success, NULL on error.
  638. */
  639. struct drm_display_mode *drm_mode_create(struct drm_device *dev)
  640. {
  641. struct drm_display_mode *nmode;
  642. nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
  643. if (!nmode)
  644. return NULL;
  645. if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
  646. kfree(nmode);
  647. return NULL;
  648. }
  649. return nmode;
  650. }
  651. EXPORT_SYMBOL(drm_mode_create);
  652. /**
  653. * drm_mode_destroy - remove a mode
  654. * @dev: DRM device
  655. * @mode: mode to remove
  656. *
  657. * LOCKING:
  658. * Caller must hold mode config lock.
  659. *
  660. * Free @mode's unique identifier, then free it.
  661. */
  662. void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
  663. {
  664. if (!mode)
  665. return;
  666. drm_mode_object_put(dev, &mode->base);
  667. kfree(mode);
  668. }
  669. EXPORT_SYMBOL(drm_mode_destroy);
  670. static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
  671. {
  672. struct drm_property *edid;
  673. struct drm_property *dpms;
  674. /*
  675. * Standard properties (apply to all connectors)
  676. */
  677. edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
  678. DRM_MODE_PROP_IMMUTABLE,
  679. "EDID", 0);
  680. dev->mode_config.edid_property = edid;
  681. dpms = drm_property_create_enum(dev, 0,
  682. "DPMS", drm_dpms_enum_list,
  683. ARRAY_SIZE(drm_dpms_enum_list));
  684. dev->mode_config.dpms_property = dpms;
  685. return 0;
  686. }
  687. /**
  688. * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
  689. * @dev: DRM device
  690. *
  691. * Called by a driver the first time a DVI-I connector is made.
  692. */
  693. int drm_mode_create_dvi_i_properties(struct drm_device *dev)
  694. {
  695. struct drm_property *dvi_i_selector;
  696. struct drm_property *dvi_i_subconnector;
  697. if (dev->mode_config.dvi_i_select_subconnector_property)
  698. return 0;
  699. dvi_i_selector =
  700. drm_property_create_enum(dev, 0,
  701. "select subconnector",
  702. drm_dvi_i_select_enum_list,
  703. ARRAY_SIZE(drm_dvi_i_select_enum_list));
  704. dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
  705. dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  706. "subconnector",
  707. drm_dvi_i_subconnector_enum_list,
  708. ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
  709. dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
  710. return 0;
  711. }
  712. EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
  713. /**
  714. * drm_create_tv_properties - create TV specific connector properties
  715. * @dev: DRM device
  716. * @num_modes: number of different TV formats (modes) supported
  717. * @modes: array of pointers to strings containing name of each format
  718. *
  719. * Called by a driver's TV initialization routine, this function creates
  720. * the TV specific connector properties for a given device. Caller is
  721. * responsible for allocating a list of format names and passing them to
  722. * this routine.
  723. */
  724. int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
  725. char *modes[])
  726. {
  727. struct drm_property *tv_selector;
  728. struct drm_property *tv_subconnector;
  729. int i;
  730. if (dev->mode_config.tv_select_subconnector_property)
  731. return 0;
  732. /*
  733. * Basic connector properties
  734. */
  735. tv_selector = drm_property_create_enum(dev, 0,
  736. "select subconnector",
  737. drm_tv_select_enum_list,
  738. ARRAY_SIZE(drm_tv_select_enum_list));
  739. dev->mode_config.tv_select_subconnector_property = tv_selector;
  740. tv_subconnector =
  741. drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  742. "subconnector",
  743. drm_tv_subconnector_enum_list,
  744. ARRAY_SIZE(drm_tv_subconnector_enum_list));
  745. dev->mode_config.tv_subconnector_property = tv_subconnector;
  746. /*
  747. * Other, TV specific properties: margins & TV modes.
  748. */
  749. dev->mode_config.tv_left_margin_property =
  750. drm_property_create_range(dev, 0, "left margin", 0, 100);
  751. dev->mode_config.tv_right_margin_property =
  752. drm_property_create_range(dev, 0, "right margin", 0, 100);
  753. dev->mode_config.tv_top_margin_property =
  754. drm_property_create_range(dev, 0, "top margin", 0, 100);
  755. dev->mode_config.tv_bottom_margin_property =
  756. drm_property_create_range(dev, 0, "bottom margin", 0, 100);
  757. dev->mode_config.tv_mode_property =
  758. drm_property_create(dev, DRM_MODE_PROP_ENUM,
  759. "mode", num_modes);
  760. for (i = 0; i < num_modes; i++)
  761. drm_property_add_enum(dev->mode_config.tv_mode_property, i,
  762. i, modes[i]);
  763. dev->mode_config.tv_brightness_property =
  764. drm_property_create_range(dev, 0, "brightness", 0, 100);
  765. dev->mode_config.tv_contrast_property =
  766. drm_property_create_range(dev, 0, "contrast", 0, 100);
  767. dev->mode_config.tv_flicker_reduction_property =
  768. drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
  769. dev->mode_config.tv_overscan_property =
  770. drm_property_create_range(dev, 0, "overscan", 0, 100);
  771. dev->mode_config.tv_saturation_property =
  772. drm_property_create_range(dev, 0, "saturation", 0, 100);
  773. dev->mode_config.tv_hue_property =
  774. drm_property_create_range(dev, 0, "hue", 0, 100);
  775. return 0;
  776. }
  777. EXPORT_SYMBOL(drm_mode_create_tv_properties);
  778. /**
  779. * drm_mode_create_scaling_mode_property - create scaling mode property
  780. * @dev: DRM device
  781. *
  782. * Called by a driver the first time it's needed, must be attached to desired
  783. * connectors.
  784. */
  785. int drm_mode_create_scaling_mode_property(struct drm_device *dev)
  786. {
  787. struct drm_property *scaling_mode;
  788. if (dev->mode_config.scaling_mode_property)
  789. return 0;
  790. scaling_mode =
  791. drm_property_create_enum(dev, 0, "scaling mode",
  792. drm_scaling_mode_enum_list,
  793. ARRAY_SIZE(drm_scaling_mode_enum_list));
  794. dev->mode_config.scaling_mode_property = scaling_mode;
  795. return 0;
  796. }
  797. EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
  798. /**
  799. * drm_mode_create_dithering_property - create dithering property
  800. * @dev: DRM device
  801. *
  802. * Called by a driver the first time it's needed, must be attached to desired
  803. * connectors.
  804. */
  805. int drm_mode_create_dithering_property(struct drm_device *dev)
  806. {
  807. struct drm_property *dithering_mode;
  808. if (dev->mode_config.dithering_mode_property)
  809. return 0;
  810. dithering_mode =
  811. drm_property_create_enum(dev, 0, "dithering",
  812. drm_dithering_mode_enum_list,
  813. ARRAY_SIZE(drm_dithering_mode_enum_list));
  814. dev->mode_config.dithering_mode_property = dithering_mode;
  815. return 0;
  816. }
  817. EXPORT_SYMBOL(drm_mode_create_dithering_property);
  818. /**
  819. * drm_mode_create_dirty_property - create dirty property
  820. * @dev: DRM device
  821. *
  822. * Called by a driver the first time it's needed, must be attached to desired
  823. * connectors.
  824. */
  825. int drm_mode_create_dirty_info_property(struct drm_device *dev)
  826. {
  827. struct drm_property *dirty_info;
  828. if (dev->mode_config.dirty_info_property)
  829. return 0;
  830. dirty_info =
  831. drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  832. "dirty",
  833. drm_dirty_info_enum_list,
  834. ARRAY_SIZE(drm_dirty_info_enum_list));
  835. dev->mode_config.dirty_info_property = dirty_info;
  836. return 0;
  837. }
  838. EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
  839. /**
  840. * drm_mode_config_init - initialize DRM mode_configuration structure
  841. * @dev: DRM device
  842. *
  843. * LOCKING:
  844. * None, should happen single threaded at init time.
  845. *
  846. * Initialize @dev's mode_config structure, used for tracking the graphics
  847. * configuration of @dev.
  848. */
  849. void drm_mode_config_init(struct drm_device *dev)
  850. {
  851. mutex_init(&dev->mode_config.mutex);
  852. mutex_init(&dev->mode_config.idr_mutex);
  853. INIT_LIST_HEAD(&dev->mode_config.fb_list);
  854. INIT_LIST_HEAD(&dev->mode_config.crtc_list);
  855. INIT_LIST_HEAD(&dev->mode_config.connector_list);
  856. INIT_LIST_HEAD(&dev->mode_config.encoder_list);
  857. INIT_LIST_HEAD(&dev->mode_config.property_list);
  858. INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
  859. INIT_LIST_HEAD(&dev->mode_config.plane_list);
  860. idr_init(&dev->mode_config.crtc_idr);
  861. mutex_lock(&dev->mode_config.mutex);
  862. drm_mode_create_standard_connector_properties(dev);
  863. mutex_unlock(&dev->mode_config.mutex);
  864. /* Just to be sure */
  865. dev->mode_config.num_fb = 0;
  866. dev->mode_config.num_connector = 0;
  867. dev->mode_config.num_crtc = 0;
  868. dev->mode_config.num_encoder = 0;
  869. }
  870. EXPORT_SYMBOL(drm_mode_config_init);
  871. int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
  872. {
  873. uint32_t total_objects = 0;
  874. total_objects += dev->mode_config.num_crtc;
  875. total_objects += dev->mode_config.num_connector;
  876. total_objects += dev->mode_config.num_encoder;
  877. group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
  878. if (!group->id_list)
  879. return -ENOMEM;
  880. group->num_crtcs = 0;
  881. group->num_connectors = 0;
  882. group->num_encoders = 0;
  883. return 0;
  884. }
  885. int drm_mode_group_init_legacy_group(struct drm_device *dev,
  886. struct drm_mode_group *group)
  887. {
  888. struct drm_crtc *crtc;
  889. struct drm_encoder *encoder;
  890. struct drm_connector *connector;
  891. int ret;
  892. if ((ret = drm_mode_group_init(dev, group)))
  893. return ret;
  894. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  895. group->id_list[group->num_crtcs++] = crtc->base.id;
  896. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  897. group->id_list[group->num_crtcs + group->num_encoders++] =
  898. encoder->base.id;
  899. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  900. group->id_list[group->num_crtcs + group->num_encoders +
  901. group->num_connectors++] = connector->base.id;
  902. return 0;
  903. }
  904. EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
  905. /**
  906. * drm_mode_config_cleanup - free up DRM mode_config info
  907. * @dev: DRM device
  908. *
  909. * LOCKING:
  910. * Caller must hold mode config lock.
  911. *
  912. * Free up all the connectors and CRTCs associated with this DRM device, then
  913. * free up the framebuffers and associated buffer objects.
  914. *
  915. * FIXME: cleanup any dangling user buffer objects too
  916. */
  917. void drm_mode_config_cleanup(struct drm_device *dev)
  918. {
  919. struct drm_connector *connector, *ot;
  920. struct drm_crtc *crtc, *ct;
  921. struct drm_encoder *encoder, *enct;
  922. struct drm_framebuffer *fb, *fbt;
  923. struct drm_property *property, *pt;
  924. struct drm_plane *plane, *plt;
  925. list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
  926. head) {
  927. encoder->funcs->destroy(encoder);
  928. }
  929. list_for_each_entry_safe(connector, ot,
  930. &dev->mode_config.connector_list, head) {
  931. connector->funcs->destroy(connector);
  932. }
  933. list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
  934. head) {
  935. drm_property_destroy(dev, property);
  936. }
  937. list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
  938. drm_framebuffer_remove(fb);
  939. }
  940. list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
  941. head) {
  942. plane->funcs->destroy(plane);
  943. }
  944. list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
  945. crtc->funcs->destroy(crtc);
  946. }
  947. idr_remove_all(&dev->mode_config.crtc_idr);
  948. idr_destroy(&dev->mode_config.crtc_idr);
  949. }
  950. EXPORT_SYMBOL(drm_mode_config_cleanup);
  951. /**
  952. * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
  953. * @out: drm_mode_modeinfo struct to return to the user
  954. * @in: drm_display_mode to use
  955. *
  956. * LOCKING:
  957. * None.
  958. *
  959. * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
  960. * the user.
  961. */
  962. static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
  963. const struct drm_display_mode *in)
  964. {
  965. WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
  966. in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
  967. in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
  968. in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
  969. in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
  970. "timing values too large for mode info\n");
  971. out->clock = in->clock;
  972. out->hdisplay = in->hdisplay;
  973. out->hsync_start = in->hsync_start;
  974. out->hsync_end = in->hsync_end;
  975. out->htotal = in->htotal;
  976. out->hskew = in->hskew;
  977. out->vdisplay = in->vdisplay;
  978. out->vsync_start = in->vsync_start;
  979. out->vsync_end = in->vsync_end;
  980. out->vtotal = in->vtotal;
  981. out->vscan = in->vscan;
  982. out->vrefresh = in->vrefresh;
  983. out->flags = in->flags;
  984. out->type = in->type;
  985. strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  986. out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  987. }
  988. /**
  989. * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
  990. * @out: drm_display_mode to return to the user
  991. * @in: drm_mode_modeinfo to use
  992. *
  993. * LOCKING:
  994. * None.
  995. *
  996. * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
  997. * the caller.
  998. *
  999. * RETURNS:
  1000. * Zero on success, errno on failure.
  1001. */
  1002. static int drm_crtc_convert_umode(struct drm_display_mode *out,
  1003. const struct drm_mode_modeinfo *in)
  1004. {
  1005. if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
  1006. return -ERANGE;
  1007. out->clock = in->clock;
  1008. out->hdisplay = in->hdisplay;
  1009. out->hsync_start = in->hsync_start;
  1010. out->hsync_end = in->hsync_end;
  1011. out->htotal = in->htotal;
  1012. out->hskew = in->hskew;
  1013. out->vdisplay = in->vdisplay;
  1014. out->vsync_start = in->vsync_start;
  1015. out->vsync_end = in->vsync_end;
  1016. out->vtotal = in->vtotal;
  1017. out->vscan = in->vscan;
  1018. out->vrefresh = in->vrefresh;
  1019. out->flags = in->flags;
  1020. out->type = in->type;
  1021. strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  1022. out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  1023. return 0;
  1024. }
  1025. /**
  1026. * drm_mode_getresources - get graphics configuration
  1027. * @inode: inode from the ioctl
  1028. * @filp: file * from the ioctl
  1029. * @cmd: cmd from ioctl
  1030. * @arg: arg from ioctl
  1031. *
  1032. * LOCKING:
  1033. * Takes mode config lock.
  1034. *
  1035. * Construct a set of configuration description structures and return
  1036. * them to the user, including CRTC, connector and framebuffer configuration.
  1037. *
  1038. * Called by the user via ioctl.
  1039. *
  1040. * RETURNS:
  1041. * Zero on success, errno on failure.
  1042. */
  1043. int drm_mode_getresources(struct drm_device *dev, void *data,
  1044. struct drm_file *file_priv)
  1045. {
  1046. struct drm_mode_card_res *card_res = data;
  1047. struct list_head *lh;
  1048. struct drm_framebuffer *fb;
  1049. struct drm_connector *connector;
  1050. struct drm_crtc *crtc;
  1051. struct drm_encoder *encoder;
  1052. int ret = 0;
  1053. int connector_count = 0;
  1054. int crtc_count = 0;
  1055. int fb_count = 0;
  1056. int encoder_count = 0;
  1057. int copied = 0, i;
  1058. uint32_t __user *fb_id;
  1059. uint32_t __user *crtc_id;
  1060. uint32_t __user *connector_id;
  1061. uint32_t __user *encoder_id;
  1062. struct drm_mode_group *mode_group;
  1063. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1064. return -EINVAL;
  1065. mutex_lock(&dev->mode_config.mutex);
  1066. /*
  1067. * For the non-control nodes we need to limit the list of resources
  1068. * by IDs in the group list for this node
  1069. */
  1070. list_for_each(lh, &file_priv->fbs)
  1071. fb_count++;
  1072. mode_group = &file_priv->master->minor->mode_group;
  1073. if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  1074. list_for_each(lh, &dev->mode_config.crtc_list)
  1075. crtc_count++;
  1076. list_for_each(lh, &dev->mode_config.connector_list)
  1077. connector_count++;
  1078. list_for_each(lh, &dev->mode_config.encoder_list)
  1079. encoder_count++;
  1080. } else {
  1081. crtc_count = mode_group->num_crtcs;
  1082. connector_count = mode_group->num_connectors;
  1083. encoder_count = mode_group->num_encoders;
  1084. }
  1085. card_res->max_height = dev->mode_config.max_height;
  1086. card_res->min_height = dev->mode_config.min_height;
  1087. card_res->max_width = dev->mode_config.max_width;
  1088. card_res->min_width = dev->mode_config.min_width;
  1089. /* handle this in 4 parts */
  1090. /* FBs */
  1091. if (card_res->count_fbs >= fb_count) {
  1092. copied = 0;
  1093. fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
  1094. list_for_each_entry(fb, &file_priv->fbs, filp_head) {
  1095. if (put_user(fb->base.id, fb_id + copied)) {
  1096. ret = -EFAULT;
  1097. goto out;
  1098. }
  1099. copied++;
  1100. }
  1101. }
  1102. card_res->count_fbs = fb_count;
  1103. /* CRTCs */
  1104. if (card_res->count_crtcs >= crtc_count) {
  1105. copied = 0;
  1106. crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
  1107. if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  1108. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  1109. head) {
  1110. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  1111. if (put_user(crtc->base.id, crtc_id + copied)) {
  1112. ret = -EFAULT;
  1113. goto out;
  1114. }
  1115. copied++;
  1116. }
  1117. } else {
  1118. for (i = 0; i < mode_group->num_crtcs; i++) {
  1119. if (put_user(mode_group->id_list[i],
  1120. crtc_id + copied)) {
  1121. ret = -EFAULT;
  1122. goto out;
  1123. }
  1124. copied++;
  1125. }
  1126. }
  1127. }
  1128. card_res->count_crtcs = crtc_count;
  1129. /* Encoders */
  1130. if (card_res->count_encoders >= encoder_count) {
  1131. copied = 0;
  1132. encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
  1133. if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  1134. list_for_each_entry(encoder,
  1135. &dev->mode_config.encoder_list,
  1136. head) {
  1137. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
  1138. drm_get_encoder_name(encoder));
  1139. if (put_user(encoder->base.id, encoder_id +
  1140. copied)) {
  1141. ret = -EFAULT;
  1142. goto out;
  1143. }
  1144. copied++;
  1145. }
  1146. } else {
  1147. for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
  1148. if (put_user(mode_group->id_list[i],
  1149. encoder_id + copied)) {
  1150. ret = -EFAULT;
  1151. goto out;
  1152. }
  1153. copied++;
  1154. }
  1155. }
  1156. }
  1157. card_res->count_encoders = encoder_count;
  1158. /* Connectors */
  1159. if (card_res->count_connectors >= connector_count) {
  1160. copied = 0;
  1161. connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
  1162. if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  1163. list_for_each_entry(connector,
  1164. &dev->mode_config.connector_list,
  1165. head) {
  1166. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  1167. connector->base.id,
  1168. drm_get_connector_name(connector));
  1169. if (put_user(connector->base.id,
  1170. connector_id + copied)) {
  1171. ret = -EFAULT;
  1172. goto out;
  1173. }
  1174. copied++;
  1175. }
  1176. } else {
  1177. int start = mode_group->num_crtcs +
  1178. mode_group->num_encoders;
  1179. for (i = start; i < start + mode_group->num_connectors; i++) {
  1180. if (put_user(mode_group->id_list[i],
  1181. connector_id + copied)) {
  1182. ret = -EFAULT;
  1183. goto out;
  1184. }
  1185. copied++;
  1186. }
  1187. }
  1188. }
  1189. card_res->count_connectors = connector_count;
  1190. DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
  1191. card_res->count_connectors, card_res->count_encoders);
  1192. out:
  1193. mutex_unlock(&dev->mode_config.mutex);
  1194. return ret;
  1195. }
  1196. /**
  1197. * drm_mode_getcrtc - get CRTC configuration
  1198. * @inode: inode from the ioctl
  1199. * @filp: file * from the ioctl
  1200. * @cmd: cmd from ioctl
  1201. * @arg: arg from ioctl
  1202. *
  1203. * LOCKING:
  1204. * Takes mode config lock.
  1205. *
  1206. * Construct a CRTC configuration structure to return to the user.
  1207. *
  1208. * Called by the user via ioctl.
  1209. *
  1210. * RETURNS:
  1211. * Zero on success, errno on failure.
  1212. */
  1213. int drm_mode_getcrtc(struct drm_device *dev,
  1214. void *data, struct drm_file *file_priv)
  1215. {
  1216. struct drm_mode_crtc *crtc_resp = data;
  1217. struct drm_crtc *crtc;
  1218. struct drm_mode_object *obj;
  1219. int ret = 0;
  1220. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1221. return -EINVAL;
  1222. mutex_lock(&dev->mode_config.mutex);
  1223. obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
  1224. DRM_MODE_OBJECT_CRTC);
  1225. if (!obj) {
  1226. ret = -EINVAL;
  1227. goto out;
  1228. }
  1229. crtc = obj_to_crtc(obj);
  1230. crtc_resp->x = crtc->x;
  1231. crtc_resp->y = crtc->y;
  1232. crtc_resp->gamma_size = crtc->gamma_size;
  1233. if (crtc->fb)
  1234. crtc_resp->fb_id = crtc->fb->base.id;
  1235. else
  1236. crtc_resp->fb_id = 0;
  1237. if (crtc->enabled) {
  1238. drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
  1239. crtc_resp->mode_valid = 1;
  1240. } else {
  1241. crtc_resp->mode_valid = 0;
  1242. }
  1243. out:
  1244. mutex_unlock(&dev->mode_config.mutex);
  1245. return ret;
  1246. }
  1247. /**
  1248. * drm_mode_getconnector - get connector configuration
  1249. * @inode: inode from the ioctl
  1250. * @filp: file * from the ioctl
  1251. * @cmd: cmd from ioctl
  1252. * @arg: arg from ioctl
  1253. *
  1254. * LOCKING:
  1255. * Takes mode config lock.
  1256. *
  1257. * Construct a connector configuration structure to return to the user.
  1258. *
  1259. * Called by the user via ioctl.
  1260. *
  1261. * RETURNS:
  1262. * Zero on success, errno on failure.
  1263. */
  1264. int drm_mode_getconnector(struct drm_device *dev, void *data,
  1265. struct drm_file *file_priv)
  1266. {
  1267. struct drm_mode_get_connector *out_resp = data;
  1268. struct drm_mode_object *obj;
  1269. struct drm_connector *connector;
  1270. struct drm_display_mode *mode;
  1271. int mode_count = 0;
  1272. int props_count = 0;
  1273. int encoders_count = 0;
  1274. int ret = 0;
  1275. int copied = 0;
  1276. int i;
  1277. struct drm_mode_modeinfo u_mode;
  1278. struct drm_mode_modeinfo __user *mode_ptr;
  1279. uint32_t __user *prop_ptr;
  1280. uint64_t __user *prop_values;
  1281. uint32_t __user *encoder_ptr;
  1282. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1283. return -EINVAL;
  1284. memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
  1285. DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
  1286. mutex_lock(&dev->mode_config.mutex);
  1287. obj = drm_mode_object_find(dev, out_resp->connector_id,
  1288. DRM_MODE_OBJECT_CONNECTOR);
  1289. if (!obj) {
  1290. ret = -EINVAL;
  1291. goto out;
  1292. }
  1293. connector = obj_to_connector(obj);
  1294. props_count = connector->properties.count;
  1295. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  1296. if (connector->encoder_ids[i] != 0) {
  1297. encoders_count++;
  1298. }
  1299. }
  1300. if (out_resp->count_modes == 0) {
  1301. connector->funcs->fill_modes(connector,
  1302. dev->mode_config.max_width,
  1303. dev->mode_config.max_height);
  1304. }
  1305. /* delayed so we get modes regardless of pre-fill_modes state */
  1306. list_for_each_entry(mode, &connector->modes, head)
  1307. mode_count++;
  1308. out_resp->connector_id = connector->base.id;
  1309. out_resp->connector_type = connector->connector_type;
  1310. out_resp->connector_type_id = connector->connector_type_id;
  1311. out_resp->mm_width = connector->display_info.width_mm;
  1312. out_resp->mm_height = connector->display_info.height_mm;
  1313. out_resp->subpixel = connector->display_info.subpixel_order;
  1314. out_resp->connection = connector->status;
  1315. if (connector->encoder)
  1316. out_resp->encoder_id = connector->encoder->base.id;
  1317. else
  1318. out_resp->encoder_id = 0;
  1319. /*
  1320. * This ioctl is called twice, once to determine how much space is
  1321. * needed, and the 2nd time to fill it.
  1322. */
  1323. if ((out_resp->count_modes >= mode_count) && mode_count) {
  1324. copied = 0;
  1325. mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
  1326. list_for_each_entry(mode, &connector->modes, head) {
  1327. drm_crtc_convert_to_umode(&u_mode, mode);
  1328. if (copy_to_user(mode_ptr + copied,
  1329. &u_mode, sizeof(u_mode))) {
  1330. ret = -EFAULT;
  1331. goto out;
  1332. }
  1333. copied++;
  1334. }
  1335. }
  1336. out_resp->count_modes = mode_count;
  1337. if ((out_resp->count_props >= props_count) && props_count) {
  1338. copied = 0;
  1339. prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
  1340. prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
  1341. for (i = 0; i < connector->properties.count; i++) {
  1342. if (put_user(connector->properties.ids[i],
  1343. prop_ptr + copied)) {
  1344. ret = -EFAULT;
  1345. goto out;
  1346. }
  1347. if (put_user(connector->properties.values[i],
  1348. prop_values + copied)) {
  1349. ret = -EFAULT;
  1350. goto out;
  1351. }
  1352. copied++;
  1353. }
  1354. }
  1355. out_resp->count_props = props_count;
  1356. if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
  1357. copied = 0;
  1358. encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
  1359. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  1360. if (connector->encoder_ids[i] != 0) {
  1361. if (put_user(connector->encoder_ids[i],
  1362. encoder_ptr + copied)) {
  1363. ret = -EFAULT;
  1364. goto out;
  1365. }
  1366. copied++;
  1367. }
  1368. }
  1369. }
  1370. out_resp->count_encoders = encoders_count;
  1371. out:
  1372. mutex_unlock(&dev->mode_config.mutex);
  1373. return ret;
  1374. }
  1375. int drm_mode_getencoder(struct drm_device *dev, void *data,
  1376. struct drm_file *file_priv)
  1377. {
  1378. struct drm_mode_get_encoder *enc_resp = data;
  1379. struct drm_mode_object *obj;
  1380. struct drm_encoder *encoder;
  1381. int ret = 0;
  1382. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1383. return -EINVAL;
  1384. mutex_lock(&dev->mode_config.mutex);
  1385. obj = drm_mode_object_find(dev, enc_resp->encoder_id,
  1386. DRM_MODE_OBJECT_ENCODER);
  1387. if (!obj) {
  1388. ret = -EINVAL;
  1389. goto out;
  1390. }
  1391. encoder = obj_to_encoder(obj);
  1392. if (encoder->crtc)
  1393. enc_resp->crtc_id = encoder->crtc->base.id;
  1394. else
  1395. enc_resp->crtc_id = 0;
  1396. enc_resp->encoder_type = encoder->encoder_type;
  1397. enc_resp->encoder_id = encoder->base.id;
  1398. enc_resp->possible_crtcs = encoder->possible_crtcs;
  1399. enc_resp->possible_clones = encoder->possible_clones;
  1400. out:
  1401. mutex_unlock(&dev->mode_config.mutex);
  1402. return ret;
  1403. }
  1404. /**
  1405. * drm_mode_getplane_res - get plane info
  1406. * @dev: DRM device
  1407. * @data: ioctl data
  1408. * @file_priv: DRM file info
  1409. *
  1410. * LOCKING:
  1411. * Takes mode config lock.
  1412. *
  1413. * Return an plane count and set of IDs.
  1414. */
  1415. int drm_mode_getplane_res(struct drm_device *dev, void *data,
  1416. struct drm_file *file_priv)
  1417. {
  1418. struct drm_mode_get_plane_res *plane_resp = data;
  1419. struct drm_mode_config *config;
  1420. struct drm_plane *plane;
  1421. uint32_t __user *plane_ptr;
  1422. int copied = 0, ret = 0;
  1423. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1424. return -EINVAL;
  1425. mutex_lock(&dev->mode_config.mutex);
  1426. config = &dev->mode_config;
  1427. /*
  1428. * This ioctl is called twice, once to determine how much space is
  1429. * needed, and the 2nd time to fill it.
  1430. */
  1431. if (config->num_plane &&
  1432. (plane_resp->count_planes >= config->num_plane)) {
  1433. plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
  1434. list_for_each_entry(plane, &config->plane_list, head) {
  1435. if (put_user(plane->base.id, plane_ptr + copied)) {
  1436. ret = -EFAULT;
  1437. goto out;
  1438. }
  1439. copied++;
  1440. }
  1441. }
  1442. plane_resp->count_planes = config->num_plane;
  1443. out:
  1444. mutex_unlock(&dev->mode_config.mutex);
  1445. return ret;
  1446. }
  1447. /**
  1448. * drm_mode_getplane - get plane info
  1449. * @dev: DRM device
  1450. * @data: ioctl data
  1451. * @file_priv: DRM file info
  1452. *
  1453. * LOCKING:
  1454. * Takes mode config lock.
  1455. *
  1456. * Return plane info, including formats supported, gamma size, any
  1457. * current fb, etc.
  1458. */
  1459. int drm_mode_getplane(struct drm_device *dev, void *data,
  1460. struct drm_file *file_priv)
  1461. {
  1462. struct drm_mode_get_plane *plane_resp = data;
  1463. struct drm_mode_object *obj;
  1464. struct drm_plane *plane;
  1465. uint32_t __user *format_ptr;
  1466. int ret = 0;
  1467. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1468. return -EINVAL;
  1469. mutex_lock(&dev->mode_config.mutex);
  1470. obj = drm_mode_object_find(dev, plane_resp->plane_id,
  1471. DRM_MODE_OBJECT_PLANE);
  1472. if (!obj) {
  1473. ret = -ENOENT;
  1474. goto out;
  1475. }
  1476. plane = obj_to_plane(obj);
  1477. if (plane->crtc)
  1478. plane_resp->crtc_id = plane->crtc->base.id;
  1479. else
  1480. plane_resp->crtc_id = 0;
  1481. if (plane->fb)
  1482. plane_resp->fb_id = plane->fb->base.id;
  1483. else
  1484. plane_resp->fb_id = 0;
  1485. plane_resp->plane_id = plane->base.id;
  1486. plane_resp->possible_crtcs = plane->possible_crtcs;
  1487. plane_resp->gamma_size = plane->gamma_size;
  1488. /*
  1489. * This ioctl is called twice, once to determine how much space is
  1490. * needed, and the 2nd time to fill it.
  1491. */
  1492. if (plane->format_count &&
  1493. (plane_resp->count_format_types >= plane->format_count)) {
  1494. format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
  1495. if (copy_to_user(format_ptr,
  1496. plane->format_types,
  1497. sizeof(uint32_t) * plane->format_count)) {
  1498. ret = -EFAULT;
  1499. goto out;
  1500. }
  1501. }
  1502. plane_resp->count_format_types = plane->format_count;
  1503. out:
  1504. mutex_unlock(&dev->mode_config.mutex);
  1505. return ret;
  1506. }
  1507. /**
  1508. * drm_mode_setplane - set up or tear down an plane
  1509. * @dev: DRM device
  1510. * @data: ioctl data*
  1511. * @file_prive: DRM file info
  1512. *
  1513. * LOCKING:
  1514. * Takes mode config lock.
  1515. *
  1516. * Set plane info, including placement, fb, scaling, and other factors.
  1517. * Or pass a NULL fb to disable.
  1518. */
  1519. int drm_mode_setplane(struct drm_device *dev, void *data,
  1520. struct drm_file *file_priv)
  1521. {
  1522. struct drm_mode_set_plane *plane_req = data;
  1523. struct drm_mode_object *obj;
  1524. struct drm_plane *plane;
  1525. struct drm_crtc *crtc;
  1526. struct drm_framebuffer *fb;
  1527. int ret = 0;
  1528. unsigned int fb_width, fb_height;
  1529. int i;
  1530. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1531. return -EINVAL;
  1532. mutex_lock(&dev->mode_config.mutex);
  1533. /*
  1534. * First, find the plane, crtc, and fb objects. If not available,
  1535. * we don't bother to call the driver.
  1536. */
  1537. obj = drm_mode_object_find(dev, plane_req->plane_id,
  1538. DRM_MODE_OBJECT_PLANE);
  1539. if (!obj) {
  1540. DRM_DEBUG_KMS("Unknown plane ID %d\n",
  1541. plane_req->plane_id);
  1542. ret = -ENOENT;
  1543. goto out;
  1544. }
  1545. plane = obj_to_plane(obj);
  1546. /* No fb means shut it down */
  1547. if (!plane_req->fb_id) {
  1548. plane->funcs->disable_plane(plane);
  1549. plane->crtc = NULL;
  1550. plane->fb = NULL;
  1551. goto out;
  1552. }
  1553. obj = drm_mode_object_find(dev, plane_req->crtc_id,
  1554. DRM_MODE_OBJECT_CRTC);
  1555. if (!obj) {
  1556. DRM_DEBUG_KMS("Unknown crtc ID %d\n",
  1557. plane_req->crtc_id);
  1558. ret = -ENOENT;
  1559. goto out;
  1560. }
  1561. crtc = obj_to_crtc(obj);
  1562. obj = drm_mode_object_find(dev, plane_req->fb_id,
  1563. DRM_MODE_OBJECT_FB);
  1564. if (!obj) {
  1565. DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
  1566. plane_req->fb_id);
  1567. ret = -ENOENT;
  1568. goto out;
  1569. }
  1570. fb = obj_to_fb(obj);
  1571. /* Check whether this plane supports the fb pixel format. */
  1572. for (i = 0; i < plane->format_count; i++)
  1573. if (fb->pixel_format == plane->format_types[i])
  1574. break;
  1575. if (i == plane->format_count) {
  1576. DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
  1577. ret = -EINVAL;
  1578. goto out;
  1579. }
  1580. fb_width = fb->width << 16;
  1581. fb_height = fb->height << 16;
  1582. /* Make sure source coordinates are inside the fb. */
  1583. if (plane_req->src_w > fb_width ||
  1584. plane_req->src_x > fb_width - plane_req->src_w ||
  1585. plane_req->src_h > fb_height ||
  1586. plane_req->src_y > fb_height - plane_req->src_h) {
  1587. DRM_DEBUG_KMS("Invalid source coordinates "
  1588. "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
  1589. plane_req->src_w >> 16,
  1590. ((plane_req->src_w & 0xffff) * 15625) >> 10,
  1591. plane_req->src_h >> 16,
  1592. ((plane_req->src_h & 0xffff) * 15625) >> 10,
  1593. plane_req->src_x >> 16,
  1594. ((plane_req->src_x & 0xffff) * 15625) >> 10,
  1595. plane_req->src_y >> 16,
  1596. ((plane_req->src_y & 0xffff) * 15625) >> 10);
  1597. ret = -ENOSPC;
  1598. goto out;
  1599. }
  1600. /* Give drivers some help against integer overflows */
  1601. if (plane_req->crtc_w > INT_MAX ||
  1602. plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
  1603. plane_req->crtc_h > INT_MAX ||
  1604. plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
  1605. DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
  1606. plane_req->crtc_w, plane_req->crtc_h,
  1607. plane_req->crtc_x, plane_req->crtc_y);
  1608. ret = -ERANGE;
  1609. goto out;
  1610. }
  1611. ret = plane->funcs->update_plane(plane, crtc, fb,
  1612. plane_req->crtc_x, plane_req->crtc_y,
  1613. plane_req->crtc_w, plane_req->crtc_h,
  1614. plane_req->src_x, plane_req->src_y,
  1615. plane_req->src_w, plane_req->src_h);
  1616. if (!ret) {
  1617. plane->crtc = crtc;
  1618. plane->fb = fb;
  1619. }
  1620. out:
  1621. mutex_unlock(&dev->mode_config.mutex);
  1622. return ret;
  1623. }
  1624. /**
  1625. * drm_mode_setcrtc - set CRTC configuration
  1626. * @inode: inode from the ioctl
  1627. * @filp: file * from the ioctl
  1628. * @cmd: cmd from ioctl
  1629. * @arg: arg from ioctl
  1630. *
  1631. * LOCKING:
  1632. * Takes mode config lock.
  1633. *
  1634. * Build a new CRTC configuration based on user request.
  1635. *
  1636. * Called by the user via ioctl.
  1637. *
  1638. * RETURNS:
  1639. * Zero on success, errno on failure.
  1640. */
  1641. int drm_mode_setcrtc(struct drm_device *dev, void *data,
  1642. struct drm_file *file_priv)
  1643. {
  1644. struct drm_mode_config *config = &dev->mode_config;
  1645. struct drm_mode_crtc *crtc_req = data;
  1646. struct drm_mode_object *obj;
  1647. struct drm_crtc *crtc;
  1648. struct drm_connector **connector_set = NULL, *connector;
  1649. struct drm_framebuffer *fb = NULL;
  1650. struct drm_display_mode *mode = NULL;
  1651. struct drm_mode_set set;
  1652. uint32_t __user *set_connectors_ptr;
  1653. int ret;
  1654. int i;
  1655. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1656. return -EINVAL;
  1657. /* For some reason crtc x/y offsets are signed internally. */
  1658. if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
  1659. return -ERANGE;
  1660. mutex_lock(&dev->mode_config.mutex);
  1661. obj = drm_mode_object_find(dev, crtc_req->crtc_id,
  1662. DRM_MODE_OBJECT_CRTC);
  1663. if (!obj) {
  1664. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
  1665. ret = -EINVAL;
  1666. goto out;
  1667. }
  1668. crtc = obj_to_crtc(obj);
  1669. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  1670. if (crtc_req->mode_valid) {
  1671. /* If we have a mode we need a framebuffer. */
  1672. /* If we pass -1, set the mode with the currently bound fb */
  1673. if (crtc_req->fb_id == -1) {
  1674. if (!crtc->fb) {
  1675. DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
  1676. ret = -EINVAL;
  1677. goto out;
  1678. }
  1679. fb = crtc->fb;
  1680. } else {
  1681. obj = drm_mode_object_find(dev, crtc_req->fb_id,
  1682. DRM_MODE_OBJECT_FB);
  1683. if (!obj) {
  1684. DRM_DEBUG_KMS("Unknown FB ID%d\n",
  1685. crtc_req->fb_id);
  1686. ret = -EINVAL;
  1687. goto out;
  1688. }
  1689. fb = obj_to_fb(obj);
  1690. }
  1691. mode = drm_mode_create(dev);
  1692. if (!mode) {
  1693. ret = -ENOMEM;
  1694. goto out;
  1695. }
  1696. ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
  1697. if (ret) {
  1698. DRM_DEBUG_KMS("Invalid mode\n");
  1699. goto out;
  1700. }
  1701. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  1702. if (mode->hdisplay > fb->width ||
  1703. mode->vdisplay > fb->height ||
  1704. crtc_req->x > fb->width - mode->hdisplay ||
  1705. crtc_req->y > fb->height - mode->vdisplay) {
  1706. DRM_DEBUG_KMS("Invalid CRTC viewport %ux%u+%u+%u for fb size %ux%u.\n",
  1707. mode->hdisplay, mode->vdisplay,
  1708. crtc_req->x, crtc_req->y,
  1709. fb->width, fb->height);
  1710. ret = -ENOSPC;
  1711. goto out;
  1712. }
  1713. }
  1714. if (crtc_req->count_connectors == 0 && mode) {
  1715. DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
  1716. ret = -EINVAL;
  1717. goto out;
  1718. }
  1719. if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
  1720. DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
  1721. crtc_req->count_connectors);
  1722. ret = -EINVAL;
  1723. goto out;
  1724. }
  1725. if (crtc_req->count_connectors > 0) {
  1726. u32 out_id;
  1727. /* Avoid unbounded kernel memory allocation */
  1728. if (crtc_req->count_connectors > config->num_connector) {
  1729. ret = -EINVAL;
  1730. goto out;
  1731. }
  1732. connector_set = kmalloc(crtc_req->count_connectors *
  1733. sizeof(struct drm_connector *),
  1734. GFP_KERNEL);
  1735. if (!connector_set) {
  1736. ret = -ENOMEM;
  1737. goto out;
  1738. }
  1739. for (i = 0; i < crtc_req->count_connectors; i++) {
  1740. set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
  1741. if (get_user(out_id, &set_connectors_ptr[i])) {
  1742. ret = -EFAULT;
  1743. goto out;
  1744. }
  1745. obj = drm_mode_object_find(dev, out_id,
  1746. DRM_MODE_OBJECT_CONNECTOR);
  1747. if (!obj) {
  1748. DRM_DEBUG_KMS("Connector id %d unknown\n",
  1749. out_id);
  1750. ret = -EINVAL;
  1751. goto out;
  1752. }
  1753. connector = obj_to_connector(obj);
  1754. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  1755. connector->base.id,
  1756. drm_get_connector_name(connector));
  1757. connector_set[i] = connector;
  1758. }
  1759. }
  1760. set.crtc = crtc;
  1761. set.x = crtc_req->x;
  1762. set.y = crtc_req->y;
  1763. set.mode = mode;
  1764. set.connectors = connector_set;
  1765. set.num_connectors = crtc_req->count_connectors;
  1766. set.fb = fb;
  1767. ret = crtc->funcs->set_config(&set);
  1768. out:
  1769. kfree(connector_set);
  1770. drm_mode_destroy(dev, mode);
  1771. mutex_unlock(&dev->mode_config.mutex);
  1772. return ret;
  1773. }
  1774. int drm_mode_cursor_ioctl(struct drm_device *dev,
  1775. void *data, struct drm_file *file_priv)
  1776. {
  1777. struct drm_mode_cursor *req = data;
  1778. struct drm_mode_object *obj;
  1779. struct drm_crtc *crtc;
  1780. int ret = 0;
  1781. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1782. return -EINVAL;
  1783. if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
  1784. return -EINVAL;
  1785. mutex_lock(&dev->mode_config.mutex);
  1786. obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
  1787. if (!obj) {
  1788. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
  1789. ret = -EINVAL;
  1790. goto out;
  1791. }
  1792. crtc = obj_to_crtc(obj);
  1793. if (req->flags & DRM_MODE_CURSOR_BO) {
  1794. if (!crtc->funcs->cursor_set) {
  1795. ret = -ENXIO;
  1796. goto out;
  1797. }
  1798. /* Turns off the cursor if handle is 0 */
  1799. ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
  1800. req->width, req->height);
  1801. }
  1802. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  1803. if (crtc->funcs->cursor_move) {
  1804. ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
  1805. } else {
  1806. ret = -EFAULT;
  1807. goto out;
  1808. }
  1809. }
  1810. out:
  1811. mutex_unlock(&dev->mode_config.mutex);
  1812. return ret;
  1813. }
  1814. /* Original addfb only supported RGB formats, so figure out which one */
  1815. uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
  1816. {
  1817. uint32_t fmt;
  1818. switch (bpp) {
  1819. case 8:
  1820. fmt = DRM_FORMAT_RGB332;
  1821. break;
  1822. case 16:
  1823. if (depth == 15)
  1824. fmt = DRM_FORMAT_XRGB1555;
  1825. else
  1826. fmt = DRM_FORMAT_RGB565;
  1827. break;
  1828. case 24:
  1829. fmt = DRM_FORMAT_RGB888;
  1830. break;
  1831. case 32:
  1832. if (depth == 24)
  1833. fmt = DRM_FORMAT_XRGB8888;
  1834. else if (depth == 30)
  1835. fmt = DRM_FORMAT_XRGB2101010;
  1836. else
  1837. fmt = DRM_FORMAT_ARGB8888;
  1838. break;
  1839. default:
  1840. DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
  1841. fmt = DRM_FORMAT_XRGB8888;
  1842. break;
  1843. }
  1844. return fmt;
  1845. }
  1846. EXPORT_SYMBOL(drm_mode_legacy_fb_format);
  1847. /**
  1848. * drm_mode_addfb - add an FB to the graphics configuration
  1849. * @inode: inode from the ioctl
  1850. * @filp: file * from the ioctl
  1851. * @cmd: cmd from ioctl
  1852. * @arg: arg from ioctl
  1853. *
  1854. * LOCKING:
  1855. * Takes mode config lock.
  1856. *
  1857. * Add a new FB to the specified CRTC, given a user request.
  1858. *
  1859. * Called by the user via ioctl.
  1860. *
  1861. * RETURNS:
  1862. * Zero on success, errno on failure.
  1863. */
  1864. int drm_mode_addfb(struct drm_device *dev,
  1865. void *data, struct drm_file *file_priv)
  1866. {
  1867. struct drm_mode_fb_cmd *or = data;
  1868. struct drm_mode_fb_cmd2 r = {};
  1869. struct drm_mode_config *config = &dev->mode_config;
  1870. struct drm_framebuffer *fb;
  1871. int ret = 0;
  1872. /* Use new struct with format internally */
  1873. r.fb_id = or->fb_id;
  1874. r.width = or->width;
  1875. r.height = or->height;
  1876. r.pitches[0] = or->pitch;
  1877. r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
  1878. r.handles[0] = or->handle;
  1879. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1880. return -EINVAL;
  1881. if ((config->min_width > r.width) || (r.width > config->max_width))
  1882. return -EINVAL;
  1883. if ((config->min_height > r.height) || (r.height > config->max_height))
  1884. return -EINVAL;
  1885. mutex_lock(&dev->mode_config.mutex);
  1886. /* TODO check buffer is sufficiently large */
  1887. /* TODO setup destructor callback */
  1888. fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
  1889. if (IS_ERR(fb)) {
  1890. DRM_DEBUG_KMS("could not create framebuffer\n");
  1891. ret = PTR_ERR(fb);
  1892. goto out;
  1893. }
  1894. or->fb_id = fb->base.id;
  1895. list_add(&fb->filp_head, &file_priv->fbs);
  1896. DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
  1897. out:
  1898. mutex_unlock(&dev->mode_config.mutex);
  1899. return ret;
  1900. }
  1901. static int format_check(const struct drm_mode_fb_cmd2 *r)
  1902. {
  1903. uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
  1904. switch (format) {
  1905. case DRM_FORMAT_C8:
  1906. case DRM_FORMAT_RGB332:
  1907. case DRM_FORMAT_BGR233:
  1908. case DRM_FORMAT_XRGB4444:
  1909. case DRM_FORMAT_XBGR4444:
  1910. case DRM_FORMAT_RGBX4444:
  1911. case DRM_FORMAT_BGRX4444:
  1912. case DRM_FORMAT_ARGB4444:
  1913. case DRM_FORMAT_ABGR4444:
  1914. case DRM_FORMAT_RGBA4444:
  1915. case DRM_FORMAT_BGRA4444:
  1916. case DRM_FORMAT_XRGB1555:
  1917. case DRM_FORMAT_XBGR1555:
  1918. case DRM_FORMAT_RGBX5551:
  1919. case DRM_FORMAT_BGRX5551:
  1920. case DRM_FORMAT_ARGB1555:
  1921. case DRM_FORMAT_ABGR1555:
  1922. case DRM_FORMAT_RGBA5551:
  1923. case DRM_FORMAT_BGRA5551:
  1924. case DRM_FORMAT_RGB565:
  1925. case DRM_FORMAT_BGR565:
  1926. case DRM_FORMAT_RGB888:
  1927. case DRM_FORMAT_BGR888:
  1928. case DRM_FORMAT_XRGB8888:
  1929. case DRM_FORMAT_XBGR8888:
  1930. case DRM_FORMAT_RGBX8888:
  1931. case DRM_FORMAT_BGRX8888:
  1932. case DRM_FORMAT_ARGB8888:
  1933. case DRM_FORMAT_ABGR8888:
  1934. case DRM_FORMAT_RGBA8888:
  1935. case DRM_FORMAT_BGRA8888:
  1936. case DRM_FORMAT_XRGB2101010:
  1937. case DRM_FORMAT_XBGR2101010:
  1938. case DRM_FORMAT_RGBX1010102:
  1939. case DRM_FORMAT_BGRX1010102:
  1940. case DRM_FORMAT_ARGB2101010:
  1941. case DRM_FORMAT_ABGR2101010:
  1942. case DRM_FORMAT_RGBA1010102:
  1943. case DRM_FORMAT_BGRA1010102:
  1944. case DRM_FORMAT_YUYV:
  1945. case DRM_FORMAT_YVYU:
  1946. case DRM_FORMAT_UYVY:
  1947. case DRM_FORMAT_VYUY:
  1948. case DRM_FORMAT_AYUV:
  1949. case DRM_FORMAT_NV12:
  1950. case DRM_FORMAT_NV21:
  1951. case DRM_FORMAT_NV16:
  1952. case DRM_FORMAT_NV61:
  1953. case DRM_FORMAT_NV24:
  1954. case DRM_FORMAT_NV42:
  1955. case DRM_FORMAT_YUV410:
  1956. case DRM_FORMAT_YVU410:
  1957. case DRM_FORMAT_YUV411:
  1958. case DRM_FORMAT_YVU411:
  1959. case DRM_FORMAT_YUV420:
  1960. case DRM_FORMAT_YVU420:
  1961. case DRM_FORMAT_YUV422:
  1962. case DRM_FORMAT_YVU422:
  1963. case DRM_FORMAT_YUV444:
  1964. case DRM_FORMAT_YVU444:
  1965. return 0;
  1966. default:
  1967. return -EINVAL;
  1968. }
  1969. }
  1970. static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
  1971. {
  1972. int ret, hsub, vsub, num_planes, i;
  1973. ret = format_check(r);
  1974. if (ret) {
  1975. DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
  1976. return ret;
  1977. }
  1978. hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
  1979. vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
  1980. num_planes = drm_format_num_planes(r->pixel_format);
  1981. if (r->width == 0 || r->width % hsub) {
  1982. DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
  1983. return -EINVAL;
  1984. }
  1985. if (r->height == 0 || r->height % vsub) {
  1986. DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
  1987. return -EINVAL;
  1988. }
  1989. for (i = 0; i < num_planes; i++) {
  1990. unsigned int width = r->width / (i != 0 ? hsub : 1);
  1991. if (!r->handles[i]) {
  1992. DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
  1993. return -EINVAL;
  1994. }
  1995. if (r->pitches[i] < drm_format_plane_cpp(r->pixel_format, i) * width) {
  1996. DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
  1997. return -EINVAL;
  1998. }
  1999. }
  2000. return 0;
  2001. }
  2002. /**
  2003. * drm_mode_addfb2 - add an FB to the graphics configuration
  2004. * @inode: inode from the ioctl
  2005. * @filp: file * from the ioctl
  2006. * @cmd: cmd from ioctl
  2007. * @arg: arg from ioctl
  2008. *
  2009. * LOCKING:
  2010. * Takes mode config lock.
  2011. *
  2012. * Add a new FB to the specified CRTC, given a user request with format.
  2013. *
  2014. * Called by the user via ioctl.
  2015. *
  2016. * RETURNS:
  2017. * Zero on success, errno on failure.
  2018. */
  2019. int drm_mode_addfb2(struct drm_device *dev,
  2020. void *data, struct drm_file *file_priv)
  2021. {
  2022. struct drm_mode_fb_cmd2 *r = data;
  2023. struct drm_mode_config *config = &dev->mode_config;
  2024. struct drm_framebuffer *fb;
  2025. int ret;
  2026. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2027. return -EINVAL;
  2028. if ((config->min_width > r->width) || (r->width > config->max_width)) {
  2029. DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
  2030. r->width, config->min_width, config->max_width);
  2031. return -EINVAL;
  2032. }
  2033. if ((config->min_height > r->height) || (r->height > config->max_height)) {
  2034. DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
  2035. r->height, config->min_height, config->max_height);
  2036. return -EINVAL;
  2037. }
  2038. ret = framebuffer_check(r);
  2039. if (ret)
  2040. return ret;
  2041. mutex_lock(&dev->mode_config.mutex);
  2042. fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
  2043. if (IS_ERR(fb)) {
  2044. DRM_DEBUG_KMS("could not create framebuffer\n");
  2045. ret = PTR_ERR(fb);
  2046. goto out;
  2047. }
  2048. r->fb_id = fb->base.id;
  2049. list_add(&fb->filp_head, &file_priv->fbs);
  2050. DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
  2051. out:
  2052. mutex_unlock(&dev->mode_config.mutex);
  2053. return ret;
  2054. }
  2055. /**
  2056. * drm_mode_rmfb - remove an FB from the configuration
  2057. * @inode: inode from the ioctl
  2058. * @filp: file * from the ioctl
  2059. * @cmd: cmd from ioctl
  2060. * @arg: arg from ioctl
  2061. *
  2062. * LOCKING:
  2063. * Takes mode config lock.
  2064. *
  2065. * Remove the FB specified by the user.
  2066. *
  2067. * Called by the user via ioctl.
  2068. *
  2069. * RETURNS:
  2070. * Zero on success, errno on failure.
  2071. */
  2072. int drm_mode_rmfb(struct drm_device *dev,
  2073. void *data, struct drm_file *file_priv)
  2074. {
  2075. struct drm_mode_object *obj;
  2076. struct drm_framebuffer *fb = NULL;
  2077. struct drm_framebuffer *fbl = NULL;
  2078. uint32_t *id = data;
  2079. int ret = 0;
  2080. int found = 0;
  2081. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2082. return -EINVAL;
  2083. mutex_lock(&dev->mode_config.mutex);
  2084. obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
  2085. /* TODO check that we really get a framebuffer back. */
  2086. if (!obj) {
  2087. ret = -EINVAL;
  2088. goto out;
  2089. }
  2090. fb = obj_to_fb(obj);
  2091. list_for_each_entry(fbl, &file_priv->fbs, filp_head)
  2092. if (fb == fbl)
  2093. found = 1;
  2094. if (!found) {
  2095. ret = -EINVAL;
  2096. goto out;
  2097. }
  2098. drm_framebuffer_remove(fb);
  2099. out:
  2100. mutex_unlock(&dev->mode_config.mutex);
  2101. return ret;
  2102. }
  2103. /**
  2104. * drm_mode_getfb - get FB info
  2105. * @inode: inode from the ioctl
  2106. * @filp: file * from the ioctl
  2107. * @cmd: cmd from ioctl
  2108. * @arg: arg from ioctl
  2109. *
  2110. * LOCKING:
  2111. * Takes mode config lock.
  2112. *
  2113. * Lookup the FB given its ID and return info about it.
  2114. *
  2115. * Called by the user via ioctl.
  2116. *
  2117. * RETURNS:
  2118. * Zero on success, errno on failure.
  2119. */
  2120. int drm_mode_getfb(struct drm_device *dev,
  2121. void *data, struct drm_file *file_priv)
  2122. {
  2123. struct drm_mode_fb_cmd *r = data;
  2124. struct drm_mode_object *obj;
  2125. struct drm_framebuffer *fb;
  2126. int ret = 0;
  2127. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2128. return -EINVAL;
  2129. mutex_lock(&dev->mode_config.mutex);
  2130. obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
  2131. if (!obj) {
  2132. ret = -EINVAL;
  2133. goto out;
  2134. }
  2135. fb = obj_to_fb(obj);
  2136. r->height = fb->height;
  2137. r->width = fb->width;
  2138. r->depth = fb->depth;
  2139. r->bpp = fb->bits_per_pixel;
  2140. r->pitch = fb->pitches[0];
  2141. fb->funcs->create_handle(fb, file_priv, &r->handle);
  2142. out:
  2143. mutex_unlock(&dev->mode_config.mutex);
  2144. return ret;
  2145. }
  2146. int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
  2147. void *data, struct drm_file *file_priv)
  2148. {
  2149. struct drm_clip_rect __user *clips_ptr;
  2150. struct drm_clip_rect *clips = NULL;
  2151. struct drm_mode_fb_dirty_cmd *r = data;
  2152. struct drm_mode_object *obj;
  2153. struct drm_framebuffer *fb;
  2154. unsigned flags;
  2155. int num_clips;
  2156. int ret;
  2157. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2158. return -EINVAL;
  2159. mutex_lock(&dev->mode_config.mutex);
  2160. obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
  2161. if (!obj) {
  2162. ret = -EINVAL;
  2163. goto out_err1;
  2164. }
  2165. fb = obj_to_fb(obj);
  2166. num_clips = r->num_clips;
  2167. clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
  2168. if (!num_clips != !clips_ptr) {
  2169. ret = -EINVAL;
  2170. goto out_err1;
  2171. }
  2172. flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
  2173. /* If userspace annotates copy, clips must come in pairs */
  2174. if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
  2175. ret = -EINVAL;
  2176. goto out_err1;
  2177. }
  2178. if (num_clips && clips_ptr) {
  2179. if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
  2180. ret = -EINVAL;
  2181. goto out_err1;
  2182. }
  2183. clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
  2184. if (!clips) {
  2185. ret = -ENOMEM;
  2186. goto out_err1;
  2187. }
  2188. ret = copy_from_user(clips, clips_ptr,
  2189. num_clips * sizeof(*clips));
  2190. if (ret) {
  2191. ret = -EFAULT;
  2192. goto out_err2;
  2193. }
  2194. }
  2195. if (fb->funcs->dirty) {
  2196. ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
  2197. clips, num_clips);
  2198. } else {
  2199. ret = -ENOSYS;
  2200. goto out_err2;
  2201. }
  2202. out_err2:
  2203. kfree(clips);
  2204. out_err1:
  2205. mutex_unlock(&dev->mode_config.mutex);
  2206. return ret;
  2207. }
  2208. /**
  2209. * drm_fb_release - remove and free the FBs on this file
  2210. * @filp: file * from the ioctl
  2211. *
  2212. * LOCKING:
  2213. * Takes mode config lock.
  2214. *
  2215. * Destroy all the FBs associated with @filp.
  2216. *
  2217. * Called by the user via ioctl.
  2218. *
  2219. * RETURNS:
  2220. * Zero on success, errno on failure.
  2221. */
  2222. void drm_fb_release(struct drm_file *priv)
  2223. {
  2224. struct drm_device *dev = priv->minor->dev;
  2225. struct drm_framebuffer *fb, *tfb;
  2226. mutex_lock(&dev->mode_config.mutex);
  2227. list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
  2228. drm_framebuffer_remove(fb);
  2229. }
  2230. mutex_unlock(&dev->mode_config.mutex);
  2231. }
  2232. /**
  2233. * drm_mode_attachmode - add a mode to the user mode list
  2234. * @dev: DRM device
  2235. * @connector: connector to add the mode to
  2236. * @mode: mode to add
  2237. *
  2238. * Add @mode to @connector's user mode list.
  2239. */
  2240. static void drm_mode_attachmode(struct drm_device *dev,
  2241. struct drm_connector *connector,
  2242. struct drm_display_mode *mode)
  2243. {
  2244. list_add_tail(&mode->head, &connector->user_modes);
  2245. }
  2246. int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
  2247. const struct drm_display_mode *mode)
  2248. {
  2249. struct drm_connector *connector;
  2250. int ret = 0;
  2251. struct drm_display_mode *dup_mode, *next;
  2252. LIST_HEAD(list);
  2253. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2254. if (!connector->encoder)
  2255. continue;
  2256. if (connector->encoder->crtc == crtc) {
  2257. dup_mode = drm_mode_duplicate(dev, mode);
  2258. if (!dup_mode) {
  2259. ret = -ENOMEM;
  2260. goto out;
  2261. }
  2262. list_add_tail(&dup_mode->head, &list);
  2263. }
  2264. }
  2265. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2266. if (!connector->encoder)
  2267. continue;
  2268. if (connector->encoder->crtc == crtc)
  2269. list_move_tail(list.next, &connector->user_modes);
  2270. }
  2271. WARN_ON(!list_empty(&list));
  2272. out:
  2273. list_for_each_entry_safe(dup_mode, next, &list, head)
  2274. drm_mode_destroy(dev, dup_mode);
  2275. return ret;
  2276. }
  2277. EXPORT_SYMBOL(drm_mode_attachmode_crtc);
  2278. static int drm_mode_detachmode(struct drm_device *dev,
  2279. struct drm_connector *connector,
  2280. struct drm_display_mode *mode)
  2281. {
  2282. int found = 0;
  2283. int ret = 0;
  2284. struct drm_display_mode *match_mode, *t;
  2285. list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
  2286. if (drm_mode_equal(match_mode, mode)) {
  2287. list_del(&match_mode->head);
  2288. drm_mode_destroy(dev, match_mode);
  2289. found = 1;
  2290. break;
  2291. }
  2292. }
  2293. if (!found)
  2294. ret = -EINVAL;
  2295. return ret;
  2296. }
  2297. int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
  2298. {
  2299. struct drm_connector *connector;
  2300. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2301. drm_mode_detachmode(dev, connector, mode);
  2302. }
  2303. return 0;
  2304. }
  2305. EXPORT_SYMBOL(drm_mode_detachmode_crtc);
  2306. /**
  2307. * drm_fb_attachmode - Attach a user mode to an connector
  2308. * @inode: inode from the ioctl
  2309. * @filp: file * from the ioctl
  2310. * @cmd: cmd from ioctl
  2311. * @arg: arg from ioctl
  2312. *
  2313. * This attaches a user specified mode to an connector.
  2314. * Called by the user via ioctl.
  2315. *
  2316. * RETURNS:
  2317. * Zero on success, errno on failure.
  2318. */
  2319. int drm_mode_attachmode_ioctl(struct drm_device *dev,
  2320. void *data, struct drm_file *file_priv)
  2321. {
  2322. struct drm_mode_mode_cmd *mode_cmd = data;
  2323. struct drm_connector *connector;
  2324. struct drm_display_mode *mode;
  2325. struct drm_mode_object *obj;
  2326. struct drm_mode_modeinfo *umode = &mode_cmd->mode;
  2327. int ret;
  2328. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2329. return -EINVAL;
  2330. mutex_lock(&dev->mode_config.mutex);
  2331. obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
  2332. if (!obj) {
  2333. ret = -EINVAL;
  2334. goto out;
  2335. }
  2336. connector = obj_to_connector(obj);
  2337. mode = drm_mode_create(dev);
  2338. if (!mode) {
  2339. ret = -ENOMEM;
  2340. goto out;
  2341. }
  2342. ret = drm_crtc_convert_umode(mode, umode);
  2343. if (ret) {
  2344. DRM_DEBUG_KMS("Invalid mode\n");
  2345. drm_mode_destroy(dev, mode);
  2346. goto out;
  2347. }
  2348. drm_mode_attachmode(dev, connector, mode);
  2349. out:
  2350. mutex_unlock(&dev->mode_config.mutex);
  2351. return ret;
  2352. }
  2353. /**
  2354. * drm_fb_detachmode - Detach a user specified mode from an connector
  2355. * @inode: inode from the ioctl
  2356. * @filp: file * from the ioctl
  2357. * @cmd: cmd from ioctl
  2358. * @arg: arg from ioctl
  2359. *
  2360. * Called by the user via ioctl.
  2361. *
  2362. * RETURNS:
  2363. * Zero on success, errno on failure.
  2364. */
  2365. int drm_mode_detachmode_ioctl(struct drm_device *dev,
  2366. void *data, struct drm_file *file_priv)
  2367. {
  2368. struct drm_mode_object *obj;
  2369. struct drm_mode_mode_cmd *mode_cmd = data;
  2370. struct drm_connector *connector;
  2371. struct drm_display_mode mode;
  2372. struct drm_mode_modeinfo *umode = &mode_cmd->mode;
  2373. int ret;
  2374. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2375. return -EINVAL;
  2376. mutex_lock(&dev->mode_config.mutex);
  2377. obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
  2378. if (!obj) {
  2379. ret = -EINVAL;
  2380. goto out;
  2381. }
  2382. connector = obj_to_connector(obj);
  2383. ret = drm_crtc_convert_umode(&mode, umode);
  2384. if (ret) {
  2385. DRM_DEBUG_KMS("Invalid mode\n");
  2386. goto out;
  2387. }
  2388. ret = drm_mode_detachmode(dev, connector, &mode);
  2389. out:
  2390. mutex_unlock(&dev->mode_config.mutex);
  2391. return ret;
  2392. }
  2393. struct drm_property *drm_property_create(struct drm_device *dev, int flags,
  2394. const char *name, int num_values)
  2395. {
  2396. struct drm_property *property = NULL;
  2397. int ret;
  2398. property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
  2399. if (!property)
  2400. return NULL;
  2401. if (num_values) {
  2402. property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
  2403. if (!property->values)
  2404. goto fail;
  2405. }
  2406. ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
  2407. if (ret)
  2408. goto fail;
  2409. property->flags = flags;
  2410. property->num_values = num_values;
  2411. INIT_LIST_HEAD(&property->enum_blob_list);
  2412. if (name) {
  2413. strncpy(property->name, name, DRM_PROP_NAME_LEN);
  2414. property->name[DRM_PROP_NAME_LEN-1] = '\0';
  2415. }
  2416. list_add_tail(&property->head, &dev->mode_config.property_list);
  2417. return property;
  2418. fail:
  2419. kfree(property->values);
  2420. kfree(property);
  2421. return NULL;
  2422. }
  2423. EXPORT_SYMBOL(drm_property_create);
  2424. struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
  2425. const char *name,
  2426. const struct drm_prop_enum_list *props,
  2427. int num_values)
  2428. {
  2429. struct drm_property *property;
  2430. int i, ret;
  2431. flags |= DRM_MODE_PROP_ENUM;
  2432. property = drm_property_create(dev, flags, name, num_values);
  2433. if (!property)
  2434. return NULL;
  2435. for (i = 0; i < num_values; i++) {
  2436. ret = drm_property_add_enum(property, i,
  2437. props[i].type,
  2438. props[i].name);
  2439. if (ret) {
  2440. drm_property_destroy(dev, property);
  2441. return NULL;
  2442. }
  2443. }
  2444. return property;
  2445. }
  2446. EXPORT_SYMBOL(drm_property_create_enum);
  2447. struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
  2448. int flags, const char *name,
  2449. const struct drm_prop_enum_list *props,
  2450. int num_values)
  2451. {
  2452. struct drm_property *property;
  2453. int i, ret;
  2454. flags |= DRM_MODE_PROP_BITMASK;
  2455. property = drm_property_create(dev, flags, name, num_values);
  2456. if (!property)
  2457. return NULL;
  2458. for (i = 0; i < num_values; i++) {
  2459. ret = drm_property_add_enum(property, i,
  2460. props[i].type,
  2461. props[i].name);
  2462. if (ret) {
  2463. drm_property_destroy(dev, property);
  2464. return NULL;
  2465. }
  2466. }
  2467. return property;
  2468. }
  2469. EXPORT_SYMBOL(drm_property_create_bitmask);
  2470. struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
  2471. const char *name,
  2472. uint64_t min, uint64_t max)
  2473. {
  2474. struct drm_property *property;
  2475. flags |= DRM_MODE_PROP_RANGE;
  2476. property = drm_property_create(dev, flags, name, 2);
  2477. if (!property)
  2478. return NULL;
  2479. property->values[0] = min;
  2480. property->values[1] = max;
  2481. return property;
  2482. }
  2483. EXPORT_SYMBOL(drm_property_create_range);
  2484. int drm_property_add_enum(struct drm_property *property, int index,
  2485. uint64_t value, const char *name)
  2486. {
  2487. struct drm_property_enum *prop_enum;
  2488. if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
  2489. return -EINVAL;
  2490. /*
  2491. * Bitmask enum properties have the additional constraint of values
  2492. * from 0 to 63
  2493. */
  2494. if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
  2495. return -EINVAL;
  2496. if (!list_empty(&property->enum_blob_list)) {
  2497. list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
  2498. if (prop_enum->value == value) {
  2499. strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  2500. prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  2501. return 0;
  2502. }
  2503. }
  2504. }
  2505. prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
  2506. if (!prop_enum)
  2507. return -ENOMEM;
  2508. strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  2509. prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  2510. prop_enum->value = value;
  2511. property->values[index] = value;
  2512. list_add_tail(&prop_enum->head, &property->enum_blob_list);
  2513. return 0;
  2514. }
  2515. EXPORT_SYMBOL(drm_property_add_enum);
  2516. void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
  2517. {
  2518. struct drm_property_enum *prop_enum, *pt;
  2519. list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
  2520. list_del(&prop_enum->head);
  2521. kfree(prop_enum);
  2522. }
  2523. if (property->num_values)
  2524. kfree(property->values);
  2525. drm_mode_object_put(dev, &property->base);
  2526. list_del(&property->head);
  2527. kfree(property);
  2528. }
  2529. EXPORT_SYMBOL(drm_property_destroy);
  2530. void drm_connector_attach_property(struct drm_connector *connector,
  2531. struct drm_property *property, uint64_t init_val)
  2532. {
  2533. drm_object_attach_property(&connector->base, property, init_val);
  2534. }
  2535. EXPORT_SYMBOL(drm_connector_attach_property);
  2536. int drm_connector_property_set_value(struct drm_connector *connector,
  2537. struct drm_property *property, uint64_t value)
  2538. {
  2539. return drm_object_property_set_value(&connector->base, property, value);
  2540. }
  2541. EXPORT_SYMBOL(drm_connector_property_set_value);
  2542. int drm_connector_property_get_value(struct drm_connector *connector,
  2543. struct drm_property *property, uint64_t *val)
  2544. {
  2545. return drm_object_property_get_value(&connector->base, property, val);
  2546. }
  2547. EXPORT_SYMBOL(drm_connector_property_get_value);
  2548. void drm_object_attach_property(struct drm_mode_object *obj,
  2549. struct drm_property *property,
  2550. uint64_t init_val)
  2551. {
  2552. int count = obj->properties->count;
  2553. if (count == DRM_OBJECT_MAX_PROPERTY) {
  2554. WARN(1, "Failed to attach object property (type: 0x%x). Please "
  2555. "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
  2556. "you see this message on the same object type.\n",
  2557. obj->type);
  2558. return;
  2559. }
  2560. obj->properties->ids[count] = property->base.id;
  2561. obj->properties->values[count] = init_val;
  2562. obj->properties->count++;
  2563. }
  2564. EXPORT_SYMBOL(drm_object_attach_property);
  2565. int drm_object_property_set_value(struct drm_mode_object *obj,
  2566. struct drm_property *property, uint64_t val)
  2567. {
  2568. int i;
  2569. for (i = 0; i < obj->properties->count; i++) {
  2570. if (obj->properties->ids[i] == property->base.id) {
  2571. obj->properties->values[i] = val;
  2572. return 0;
  2573. }
  2574. }
  2575. return -EINVAL;
  2576. }
  2577. EXPORT_SYMBOL(drm_object_property_set_value);
  2578. int drm_object_property_get_value(struct drm_mode_object *obj,
  2579. struct drm_property *property, uint64_t *val)
  2580. {
  2581. int i;
  2582. for (i = 0; i < obj->properties->count; i++) {
  2583. if (obj->properties->ids[i] == property->base.id) {
  2584. *val = obj->properties->values[i];
  2585. return 0;
  2586. }
  2587. }
  2588. return -EINVAL;
  2589. }
  2590. EXPORT_SYMBOL(drm_object_property_get_value);
  2591. int drm_mode_getproperty_ioctl(struct drm_device *dev,
  2592. void *data, struct drm_file *file_priv)
  2593. {
  2594. struct drm_mode_object *obj;
  2595. struct drm_mode_get_property *out_resp = data;
  2596. struct drm_property *property;
  2597. int enum_count = 0;
  2598. int blob_count = 0;
  2599. int value_count = 0;
  2600. int ret = 0, i;
  2601. int copied;
  2602. struct drm_property_enum *prop_enum;
  2603. struct drm_mode_property_enum __user *enum_ptr;
  2604. struct drm_property_blob *prop_blob;
  2605. uint32_t __user *blob_id_ptr;
  2606. uint64_t __user *values_ptr;
  2607. uint32_t __user *blob_length_ptr;
  2608. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2609. return -EINVAL;
  2610. mutex_lock(&dev->mode_config.mutex);
  2611. obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
  2612. if (!obj) {
  2613. ret = -EINVAL;
  2614. goto done;
  2615. }
  2616. property = obj_to_property(obj);
  2617. if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
  2618. list_for_each_entry(prop_enum, &property->enum_blob_list, head)
  2619. enum_count++;
  2620. } else if (property->flags & DRM_MODE_PROP_BLOB) {
  2621. list_for_each_entry(prop_blob, &property->enum_blob_list, head)
  2622. blob_count++;
  2623. }
  2624. value_count = property->num_values;
  2625. strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
  2626. out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
  2627. out_resp->flags = property->flags;
  2628. if ((out_resp->count_values >= value_count) && value_count) {
  2629. values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
  2630. for (i = 0; i < value_count; i++) {
  2631. if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
  2632. ret = -EFAULT;
  2633. goto done;
  2634. }
  2635. }
  2636. }
  2637. out_resp->count_values = value_count;
  2638. if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
  2639. if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
  2640. copied = 0;
  2641. enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
  2642. list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
  2643. if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
  2644. ret = -EFAULT;
  2645. goto done;
  2646. }
  2647. if (copy_to_user(&enum_ptr[copied].name,
  2648. &prop_enum->name, DRM_PROP_NAME_LEN)) {
  2649. ret = -EFAULT;
  2650. goto done;
  2651. }
  2652. copied++;
  2653. }
  2654. }
  2655. out_resp->count_enum_blobs = enum_count;
  2656. }
  2657. if (property->flags & DRM_MODE_PROP_BLOB) {
  2658. if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
  2659. copied = 0;
  2660. blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
  2661. blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
  2662. list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
  2663. if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
  2664. ret = -EFAULT;
  2665. goto done;
  2666. }
  2667. if (put_user(prop_blob->length, blob_length_ptr + copied)) {
  2668. ret = -EFAULT;
  2669. goto done;
  2670. }
  2671. copied++;
  2672. }
  2673. }
  2674. out_resp->count_enum_blobs = blob_count;
  2675. }
  2676. done:
  2677. mutex_unlock(&dev->mode_config.mutex);
  2678. return ret;
  2679. }
  2680. static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
  2681. void *data)
  2682. {
  2683. struct drm_property_blob *blob;
  2684. int ret;
  2685. if (!length || !data)
  2686. return NULL;
  2687. blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
  2688. if (!blob)
  2689. return NULL;
  2690. ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
  2691. if (ret) {
  2692. kfree(blob);
  2693. return NULL;
  2694. }
  2695. blob->length = length;
  2696. memcpy(blob->data, data, length);
  2697. list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
  2698. return blob;
  2699. }
  2700. static void drm_property_destroy_blob(struct drm_device *dev,
  2701. struct drm_property_blob *blob)
  2702. {
  2703. drm_mode_object_put(dev, &blob->base);
  2704. list_del(&blob->head);
  2705. kfree(blob);
  2706. }
  2707. int drm_mode_getblob_ioctl(struct drm_device *dev,
  2708. void *data, struct drm_file *file_priv)
  2709. {
  2710. struct drm_mode_object *obj;
  2711. struct drm_mode_get_blob *out_resp = data;
  2712. struct drm_property_blob *blob;
  2713. int ret = 0;
  2714. void __user *blob_ptr;
  2715. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2716. return -EINVAL;
  2717. mutex_lock(&dev->mode_config.mutex);
  2718. obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
  2719. if (!obj) {
  2720. ret = -EINVAL;
  2721. goto done;
  2722. }
  2723. blob = obj_to_blob(obj);
  2724. if (out_resp->length == blob->length) {
  2725. blob_ptr = (void __user *)(unsigned long)out_resp->data;
  2726. if (copy_to_user(blob_ptr, blob->data, blob->length)){
  2727. ret = -EFAULT;
  2728. goto done;
  2729. }
  2730. }
  2731. out_resp->length = blob->length;
  2732. done:
  2733. mutex_unlock(&dev->mode_config.mutex);
  2734. return ret;
  2735. }
  2736. int drm_mode_connector_update_edid_property(struct drm_connector *connector,
  2737. struct edid *edid)
  2738. {
  2739. struct drm_device *dev = connector->dev;
  2740. int ret, size;
  2741. if (connector->edid_blob_ptr)
  2742. drm_property_destroy_blob(dev, connector->edid_blob_ptr);
  2743. /* Delete edid, when there is none. */
  2744. if (!edid) {
  2745. connector->edid_blob_ptr = NULL;
  2746. ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
  2747. return ret;
  2748. }
  2749. size = EDID_LENGTH * (1 + edid->extensions);
  2750. connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
  2751. size, edid);
  2752. ret = drm_connector_property_set_value(connector,
  2753. dev->mode_config.edid_property,
  2754. connector->edid_blob_ptr->base.id);
  2755. return ret;
  2756. }
  2757. EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
  2758. static bool drm_property_change_is_valid(struct drm_property *property,
  2759. uint64_t value)
  2760. {
  2761. if (property->flags & DRM_MODE_PROP_IMMUTABLE)
  2762. return false;
  2763. if (property->flags & DRM_MODE_PROP_RANGE) {
  2764. if (value < property->values[0] || value > property->values[1])
  2765. return false;
  2766. return true;
  2767. } else if (property->flags & DRM_MODE_PROP_BITMASK) {
  2768. int i;
  2769. uint64_t valid_mask = 0;
  2770. for (i = 0; i < property->num_values; i++)
  2771. valid_mask |= (1ULL << property->values[i]);
  2772. return !(value & ~valid_mask);
  2773. } else {
  2774. int i;
  2775. for (i = 0; i < property->num_values; i++)
  2776. if (property->values[i] == value)
  2777. return true;
  2778. return false;
  2779. }
  2780. }
  2781. int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
  2782. void *data, struct drm_file *file_priv)
  2783. {
  2784. struct drm_mode_connector_set_property *conn_set_prop = data;
  2785. struct drm_mode_obj_set_property obj_set_prop = {
  2786. .value = conn_set_prop->value,
  2787. .prop_id = conn_set_prop->prop_id,
  2788. .obj_id = conn_set_prop->connector_id,
  2789. .obj_type = DRM_MODE_OBJECT_CONNECTOR
  2790. };
  2791. /* It does all the locking and checking we need */
  2792. return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
  2793. }
  2794. static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
  2795. struct drm_property *property,
  2796. uint64_t value)
  2797. {
  2798. int ret = -EINVAL;
  2799. struct drm_connector *connector = obj_to_connector(obj);
  2800. /* Do DPMS ourselves */
  2801. if (property == connector->dev->mode_config.dpms_property) {
  2802. if (connector->funcs->dpms)
  2803. (*connector->funcs->dpms)(connector, (int)value);
  2804. ret = 0;
  2805. } else if (connector->funcs->set_property)
  2806. ret = connector->funcs->set_property(connector, property, value);
  2807. /* store the property value if successful */
  2808. if (!ret)
  2809. drm_connector_property_set_value(connector, property, value);
  2810. return ret;
  2811. }
  2812. static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
  2813. struct drm_property *property,
  2814. uint64_t value)
  2815. {
  2816. int ret = -EINVAL;
  2817. struct drm_crtc *crtc = obj_to_crtc(obj);
  2818. if (crtc->funcs->set_property)
  2819. ret = crtc->funcs->set_property(crtc, property, value);
  2820. if (!ret)
  2821. drm_object_property_set_value(obj, property, value);
  2822. return ret;
  2823. }
  2824. static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
  2825. struct drm_property *property,
  2826. uint64_t value)
  2827. {
  2828. int ret = -EINVAL;
  2829. struct drm_plane *plane = obj_to_plane(obj);
  2830. if (plane->funcs->set_property)
  2831. ret = plane->funcs->set_property(plane, property, value);
  2832. if (!ret)
  2833. drm_object_property_set_value(obj, property, value);
  2834. return ret;
  2835. }
  2836. int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
  2837. struct drm_file *file_priv)
  2838. {
  2839. struct drm_mode_obj_get_properties *arg = data;
  2840. struct drm_mode_object *obj;
  2841. int ret = 0;
  2842. int i;
  2843. int copied = 0;
  2844. int props_count = 0;
  2845. uint32_t __user *props_ptr;
  2846. uint64_t __user *prop_values_ptr;
  2847. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2848. return -EINVAL;
  2849. mutex_lock(&dev->mode_config.mutex);
  2850. obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  2851. if (!obj) {
  2852. ret = -EINVAL;
  2853. goto out;
  2854. }
  2855. if (!obj->properties) {
  2856. ret = -EINVAL;
  2857. goto out;
  2858. }
  2859. props_count = obj->properties->count;
  2860. /* This ioctl is called twice, once to determine how much space is
  2861. * needed, and the 2nd time to fill it. */
  2862. if ((arg->count_props >= props_count) && props_count) {
  2863. copied = 0;
  2864. props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
  2865. prop_values_ptr = (uint64_t __user *)(unsigned long)
  2866. (arg->prop_values_ptr);
  2867. for (i = 0; i < props_count; i++) {
  2868. if (put_user(obj->properties->ids[i],
  2869. props_ptr + copied)) {
  2870. ret = -EFAULT;
  2871. goto out;
  2872. }
  2873. if (put_user(obj->properties->values[i],
  2874. prop_values_ptr + copied)) {
  2875. ret = -EFAULT;
  2876. goto out;
  2877. }
  2878. copied++;
  2879. }
  2880. }
  2881. arg->count_props = props_count;
  2882. out:
  2883. mutex_unlock(&dev->mode_config.mutex);
  2884. return ret;
  2885. }
  2886. int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
  2887. struct drm_file *file_priv)
  2888. {
  2889. struct drm_mode_obj_set_property *arg = data;
  2890. struct drm_mode_object *arg_obj;
  2891. struct drm_mode_object *prop_obj;
  2892. struct drm_property *property;
  2893. int ret = -EINVAL;
  2894. int i;
  2895. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2896. return -EINVAL;
  2897. mutex_lock(&dev->mode_config.mutex);
  2898. arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  2899. if (!arg_obj)
  2900. goto out;
  2901. if (!arg_obj->properties)
  2902. goto out;
  2903. for (i = 0; i < arg_obj->properties->count; i++)
  2904. if (arg_obj->properties->ids[i] == arg->prop_id)
  2905. break;
  2906. if (i == arg_obj->properties->count)
  2907. goto out;
  2908. prop_obj = drm_mode_object_find(dev, arg->prop_id,
  2909. DRM_MODE_OBJECT_PROPERTY);
  2910. if (!prop_obj)
  2911. goto out;
  2912. property = obj_to_property(prop_obj);
  2913. if (!drm_property_change_is_valid(property, arg->value))
  2914. goto out;
  2915. switch (arg_obj->type) {
  2916. case DRM_MODE_OBJECT_CONNECTOR:
  2917. ret = drm_mode_connector_set_obj_prop(arg_obj, property,
  2918. arg->value);
  2919. break;
  2920. case DRM_MODE_OBJECT_CRTC:
  2921. ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
  2922. break;
  2923. case DRM_MODE_OBJECT_PLANE:
  2924. ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
  2925. break;
  2926. }
  2927. out:
  2928. mutex_unlock(&dev->mode_config.mutex);
  2929. return ret;
  2930. }
  2931. int drm_mode_connector_attach_encoder(struct drm_connector *connector,
  2932. struct drm_encoder *encoder)
  2933. {
  2934. int i;
  2935. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  2936. if (connector->encoder_ids[i] == 0) {
  2937. connector->encoder_ids[i] = encoder->base.id;
  2938. return 0;
  2939. }
  2940. }
  2941. return -ENOMEM;
  2942. }
  2943. EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
  2944. void drm_mode_connector_detach_encoder(struct drm_connector *connector,
  2945. struct drm_encoder *encoder)
  2946. {
  2947. int i;
  2948. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  2949. if (connector->encoder_ids[i] == encoder->base.id) {
  2950. connector->encoder_ids[i] = 0;
  2951. if (connector->encoder == encoder)
  2952. connector->encoder = NULL;
  2953. break;
  2954. }
  2955. }
  2956. }
  2957. EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
  2958. int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
  2959. int gamma_size)
  2960. {
  2961. crtc->gamma_size = gamma_size;
  2962. crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
  2963. if (!crtc->gamma_store) {
  2964. crtc->gamma_size = 0;
  2965. return -ENOMEM;
  2966. }
  2967. return 0;
  2968. }
  2969. EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
  2970. int drm_mode_gamma_set_ioctl(struct drm_device *dev,
  2971. void *data, struct drm_file *file_priv)
  2972. {
  2973. struct drm_mode_crtc_lut *crtc_lut = data;
  2974. struct drm_mode_object *obj;
  2975. struct drm_crtc *crtc;
  2976. void *r_base, *g_base, *b_base;
  2977. int size;
  2978. int ret = 0;
  2979. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2980. return -EINVAL;
  2981. mutex_lock(&dev->mode_config.mutex);
  2982. obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
  2983. if (!obj) {
  2984. ret = -EINVAL;
  2985. goto out;
  2986. }
  2987. crtc = obj_to_crtc(obj);
  2988. if (crtc->funcs->gamma_set == NULL) {
  2989. ret = -ENOSYS;
  2990. goto out;
  2991. }
  2992. /* memcpy into gamma store */
  2993. if (crtc_lut->gamma_size != crtc->gamma_size) {
  2994. ret = -EINVAL;
  2995. goto out;
  2996. }
  2997. size = crtc_lut->gamma_size * (sizeof(uint16_t));
  2998. r_base = crtc->gamma_store;
  2999. if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
  3000. ret = -EFAULT;
  3001. goto out;
  3002. }
  3003. g_base = r_base + size;
  3004. if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
  3005. ret = -EFAULT;
  3006. goto out;
  3007. }
  3008. b_base = g_base + size;
  3009. if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
  3010. ret = -EFAULT;
  3011. goto out;
  3012. }
  3013. crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
  3014. out:
  3015. mutex_unlock(&dev->mode_config.mutex);
  3016. return ret;
  3017. }
  3018. int drm_mode_gamma_get_ioctl(struct drm_device *dev,
  3019. void *data, struct drm_file *file_priv)
  3020. {
  3021. struct drm_mode_crtc_lut *crtc_lut = data;
  3022. struct drm_mode_object *obj;
  3023. struct drm_crtc *crtc;
  3024. void *r_base, *g_base, *b_base;
  3025. int size;
  3026. int ret = 0;
  3027. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3028. return -EINVAL;
  3029. mutex_lock(&dev->mode_config.mutex);
  3030. obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
  3031. if (!obj) {
  3032. ret = -EINVAL;
  3033. goto out;
  3034. }
  3035. crtc = obj_to_crtc(obj);
  3036. /* memcpy into gamma store */
  3037. if (crtc_lut->gamma_size != crtc->gamma_size) {
  3038. ret = -EINVAL;
  3039. goto out;
  3040. }
  3041. size = crtc_lut->gamma_size * (sizeof(uint16_t));
  3042. r_base = crtc->gamma_store;
  3043. if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
  3044. ret = -EFAULT;
  3045. goto out;
  3046. }
  3047. g_base = r_base + size;
  3048. if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
  3049. ret = -EFAULT;
  3050. goto out;
  3051. }
  3052. b_base = g_base + size;
  3053. if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
  3054. ret = -EFAULT;
  3055. goto out;
  3056. }
  3057. out:
  3058. mutex_unlock(&dev->mode_config.mutex);
  3059. return ret;
  3060. }
  3061. int drm_mode_page_flip_ioctl(struct drm_device *dev,
  3062. void *data, struct drm_file *file_priv)
  3063. {
  3064. struct drm_mode_crtc_page_flip *page_flip = data;
  3065. struct drm_mode_object *obj;
  3066. struct drm_crtc *crtc;
  3067. struct drm_framebuffer *fb;
  3068. struct drm_pending_vblank_event *e = NULL;
  3069. unsigned long flags;
  3070. int ret = -EINVAL;
  3071. if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
  3072. page_flip->reserved != 0)
  3073. return -EINVAL;
  3074. mutex_lock(&dev->mode_config.mutex);
  3075. obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
  3076. if (!obj)
  3077. goto out;
  3078. crtc = obj_to_crtc(obj);
  3079. if (crtc->fb == NULL) {
  3080. /* The framebuffer is currently unbound, presumably
  3081. * due to a hotplug event, that userspace has not
  3082. * yet discovered.
  3083. */
  3084. ret = -EBUSY;
  3085. goto out;
  3086. }
  3087. if (crtc->funcs->page_flip == NULL)
  3088. goto out;
  3089. obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
  3090. if (!obj)
  3091. goto out;
  3092. fb = obj_to_fb(obj);
  3093. if (crtc->mode.hdisplay > fb->width ||
  3094. crtc->mode.vdisplay > fb->height ||
  3095. crtc->x > fb->width - crtc->mode.hdisplay ||
  3096. crtc->y > fb->height - crtc->mode.vdisplay) {
  3097. DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d.\n",
  3098. fb->width, fb->height,
  3099. crtc->mode.hdisplay, crtc->mode.vdisplay,
  3100. crtc->x, crtc->y);
  3101. ret = -ENOSPC;
  3102. goto out;
  3103. }
  3104. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  3105. ret = -ENOMEM;
  3106. spin_lock_irqsave(&dev->event_lock, flags);
  3107. if (file_priv->event_space < sizeof e->event) {
  3108. spin_unlock_irqrestore(&dev->event_lock, flags);
  3109. goto out;
  3110. }
  3111. file_priv->event_space -= sizeof e->event;
  3112. spin_unlock_irqrestore(&dev->event_lock, flags);
  3113. e = kzalloc(sizeof *e, GFP_KERNEL);
  3114. if (e == NULL) {
  3115. spin_lock_irqsave(&dev->event_lock, flags);
  3116. file_priv->event_space += sizeof e->event;
  3117. spin_unlock_irqrestore(&dev->event_lock, flags);
  3118. goto out;
  3119. }
  3120. e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
  3121. e->event.base.length = sizeof e->event;
  3122. e->event.user_data = page_flip->user_data;
  3123. e->base.event = &e->event.base;
  3124. e->base.file_priv = file_priv;
  3125. e->base.destroy =
  3126. (void (*) (struct drm_pending_event *)) kfree;
  3127. }
  3128. ret = crtc->funcs->page_flip(crtc, fb, e);
  3129. if (ret) {
  3130. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  3131. spin_lock_irqsave(&dev->event_lock, flags);
  3132. file_priv->event_space += sizeof e->event;
  3133. spin_unlock_irqrestore(&dev->event_lock, flags);
  3134. kfree(e);
  3135. }
  3136. }
  3137. out:
  3138. mutex_unlock(&dev->mode_config.mutex);
  3139. return ret;
  3140. }
  3141. void drm_mode_config_reset(struct drm_device *dev)
  3142. {
  3143. struct drm_crtc *crtc;
  3144. struct drm_encoder *encoder;
  3145. struct drm_connector *connector;
  3146. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  3147. if (crtc->funcs->reset)
  3148. crtc->funcs->reset(crtc);
  3149. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  3150. if (encoder->funcs->reset)
  3151. encoder->funcs->reset(encoder);
  3152. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  3153. if (connector->funcs->reset)
  3154. connector->funcs->reset(connector);
  3155. }
  3156. EXPORT_SYMBOL(drm_mode_config_reset);
  3157. int drm_mode_create_dumb_ioctl(struct drm_device *dev,
  3158. void *data, struct drm_file *file_priv)
  3159. {
  3160. struct drm_mode_create_dumb *args = data;
  3161. if (!dev->driver->dumb_create)
  3162. return -ENOSYS;
  3163. return dev->driver->dumb_create(file_priv, dev, args);
  3164. }
  3165. int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
  3166. void *data, struct drm_file *file_priv)
  3167. {
  3168. struct drm_mode_map_dumb *args = data;
  3169. /* call driver ioctl to get mmap offset */
  3170. if (!dev->driver->dumb_map_offset)
  3171. return -ENOSYS;
  3172. return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
  3173. }
  3174. int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
  3175. void *data, struct drm_file *file_priv)
  3176. {
  3177. struct drm_mode_destroy_dumb *args = data;
  3178. if (!dev->driver->dumb_destroy)
  3179. return -ENOSYS;
  3180. return dev->driver->dumb_destroy(file_priv, dev, args->handle);
  3181. }
  3182. /*
  3183. * Just need to support RGB formats here for compat with code that doesn't
  3184. * use pixel formats directly yet.
  3185. */
  3186. void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
  3187. int *bpp)
  3188. {
  3189. switch (format) {
  3190. case DRM_FORMAT_RGB332:
  3191. case DRM_FORMAT_BGR233:
  3192. *depth = 8;
  3193. *bpp = 8;
  3194. break;
  3195. case DRM_FORMAT_XRGB1555:
  3196. case DRM_FORMAT_XBGR1555:
  3197. case DRM_FORMAT_RGBX5551:
  3198. case DRM_FORMAT_BGRX5551:
  3199. case DRM_FORMAT_ARGB1555:
  3200. case DRM_FORMAT_ABGR1555:
  3201. case DRM_FORMAT_RGBA5551:
  3202. case DRM_FORMAT_BGRA5551:
  3203. *depth = 15;
  3204. *bpp = 16;
  3205. break;
  3206. case DRM_FORMAT_RGB565:
  3207. case DRM_FORMAT_BGR565:
  3208. *depth = 16;
  3209. *bpp = 16;
  3210. break;
  3211. case DRM_FORMAT_RGB888:
  3212. case DRM_FORMAT_BGR888:
  3213. *depth = 24;
  3214. *bpp = 24;
  3215. break;
  3216. case DRM_FORMAT_XRGB8888:
  3217. case DRM_FORMAT_XBGR8888:
  3218. case DRM_FORMAT_RGBX8888:
  3219. case DRM_FORMAT_BGRX8888:
  3220. *depth = 24;
  3221. *bpp = 32;
  3222. break;
  3223. case DRM_FORMAT_XRGB2101010:
  3224. case DRM_FORMAT_XBGR2101010:
  3225. case DRM_FORMAT_RGBX1010102:
  3226. case DRM_FORMAT_BGRX1010102:
  3227. case DRM_FORMAT_ARGB2101010:
  3228. case DRM_FORMAT_ABGR2101010:
  3229. case DRM_FORMAT_RGBA1010102:
  3230. case DRM_FORMAT_BGRA1010102:
  3231. *depth = 30;
  3232. *bpp = 32;
  3233. break;
  3234. case DRM_FORMAT_ARGB8888:
  3235. case DRM_FORMAT_ABGR8888:
  3236. case DRM_FORMAT_RGBA8888:
  3237. case DRM_FORMAT_BGRA8888:
  3238. *depth = 32;
  3239. *bpp = 32;
  3240. break;
  3241. default:
  3242. DRM_DEBUG_KMS("unsupported pixel format\n");
  3243. *depth = 0;
  3244. *bpp = 0;
  3245. break;
  3246. }
  3247. }
  3248. EXPORT_SYMBOL(drm_fb_get_bpp_depth);
  3249. /**
  3250. * drm_format_num_planes - get the number of planes for format
  3251. * @format: pixel format (DRM_FORMAT_*)
  3252. *
  3253. * RETURNS:
  3254. * The number of planes used by the specified pixel format.
  3255. */
  3256. int drm_format_num_planes(uint32_t format)
  3257. {
  3258. switch (format) {
  3259. case DRM_FORMAT_YUV410:
  3260. case DRM_FORMAT_YVU410:
  3261. case DRM_FORMAT_YUV411:
  3262. case DRM_FORMAT_YVU411:
  3263. case DRM_FORMAT_YUV420:
  3264. case DRM_FORMAT_YVU420:
  3265. case DRM_FORMAT_YUV422:
  3266. case DRM_FORMAT_YVU422:
  3267. case DRM_FORMAT_YUV444:
  3268. case DRM_FORMAT_YVU444:
  3269. return 3;
  3270. case DRM_FORMAT_NV12:
  3271. case DRM_FORMAT_NV21:
  3272. case DRM_FORMAT_NV16:
  3273. case DRM_FORMAT_NV61:
  3274. case DRM_FORMAT_NV24:
  3275. case DRM_FORMAT_NV42:
  3276. return 2;
  3277. default:
  3278. return 1;
  3279. }
  3280. }
  3281. EXPORT_SYMBOL(drm_format_num_planes);
  3282. /**
  3283. * drm_format_plane_cpp - determine the bytes per pixel value
  3284. * @format: pixel format (DRM_FORMAT_*)
  3285. * @plane: plane index
  3286. *
  3287. * RETURNS:
  3288. * The bytes per pixel value for the specified plane.
  3289. */
  3290. int drm_format_plane_cpp(uint32_t format, int plane)
  3291. {
  3292. unsigned int depth;
  3293. int bpp;
  3294. if (plane >= drm_format_num_planes(format))
  3295. return 0;
  3296. switch (format) {
  3297. case DRM_FORMAT_YUYV:
  3298. case DRM_FORMAT_YVYU:
  3299. case DRM_FORMAT_UYVY:
  3300. case DRM_FORMAT_VYUY:
  3301. return 2;
  3302. case DRM_FORMAT_NV12:
  3303. case DRM_FORMAT_NV21:
  3304. case DRM_FORMAT_NV16:
  3305. case DRM_FORMAT_NV61:
  3306. case DRM_FORMAT_NV24:
  3307. case DRM_FORMAT_NV42:
  3308. return plane ? 2 : 1;
  3309. case DRM_FORMAT_YUV410:
  3310. case DRM_FORMAT_YVU410:
  3311. case DRM_FORMAT_YUV411:
  3312. case DRM_FORMAT_YVU411:
  3313. case DRM_FORMAT_YUV420:
  3314. case DRM_FORMAT_YVU420:
  3315. case DRM_FORMAT_YUV422:
  3316. case DRM_FORMAT_YVU422:
  3317. case DRM_FORMAT_YUV444:
  3318. case DRM_FORMAT_YVU444:
  3319. return 1;
  3320. default:
  3321. drm_fb_get_bpp_depth(format, &depth, &bpp);
  3322. return bpp >> 3;
  3323. }
  3324. }
  3325. EXPORT_SYMBOL(drm_format_plane_cpp);
  3326. /**
  3327. * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
  3328. * @format: pixel format (DRM_FORMAT_*)
  3329. *
  3330. * RETURNS:
  3331. * The horizontal chroma subsampling factor for the
  3332. * specified pixel format.
  3333. */
  3334. int drm_format_horz_chroma_subsampling(uint32_t format)
  3335. {
  3336. switch (format) {
  3337. case DRM_FORMAT_YUV411:
  3338. case DRM_FORMAT_YVU411:
  3339. case DRM_FORMAT_YUV410:
  3340. case DRM_FORMAT_YVU410:
  3341. return 4;
  3342. case DRM_FORMAT_YUYV:
  3343. case DRM_FORMAT_YVYU:
  3344. case DRM_FORMAT_UYVY:
  3345. case DRM_FORMAT_VYUY:
  3346. case DRM_FORMAT_NV12:
  3347. case DRM_FORMAT_NV21:
  3348. case DRM_FORMAT_NV16:
  3349. case DRM_FORMAT_NV61:
  3350. case DRM_FORMAT_YUV422:
  3351. case DRM_FORMAT_YVU422:
  3352. case DRM_FORMAT_YUV420:
  3353. case DRM_FORMAT_YVU420:
  3354. return 2;
  3355. default:
  3356. return 1;
  3357. }
  3358. }
  3359. EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
  3360. /**
  3361. * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
  3362. * @format: pixel format (DRM_FORMAT_*)
  3363. *
  3364. * RETURNS:
  3365. * The vertical chroma subsampling factor for the
  3366. * specified pixel format.
  3367. */
  3368. int drm_format_vert_chroma_subsampling(uint32_t format)
  3369. {
  3370. switch (format) {
  3371. case DRM_FORMAT_YUV410:
  3372. case DRM_FORMAT_YVU410:
  3373. return 4;
  3374. case DRM_FORMAT_YUV420:
  3375. case DRM_FORMAT_YVU420:
  3376. case DRM_FORMAT_NV12:
  3377. case DRM_FORMAT_NV21:
  3378. return 2;
  3379. default:
  3380. return 1;
  3381. }
  3382. }
  3383. EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);