drm_crtc.c 90 KB

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