drm_crtc.c 81 KB

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