drm_crtc.c 97 KB

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