drm_crtc.c 90 KB

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