msm_drv.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058
  1. /*
  2. * Copyright (c) 2016-2019, The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2013 Red Hat
  4. * Author: Rob Clark <[email protected]>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. * Copyright (c) 2016 Intel Corporation
  20. *
  21. * Permission to use, copy, modify, distribute, and sell this software and its
  22. * documentation for any purpose is hereby granted without fee, provided that
  23. * the above copyright notice appear in all copies and that both that copyright
  24. * notice and this permission notice appear in supporting documentation, and
  25. * that the name of the copyright holders not be used in advertising or
  26. * publicity pertaining to distribution of the software without specific,
  27. * written prior permission. The copyright holders make no representations
  28. * about the suitability of this software for any purpose. It is provided "as
  29. * is" without express or implied warranty.
  30. *
  31. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  32. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  33. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  34. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  35. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  36. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  37. * OF THIS SOFTWARE.
  38. */
  39. #include <linux/of_address.h>
  40. #include <linux/kthread.h>
  41. #include <uapi/linux/sched/types.h>
  42. #include <drm/drm_of.h>
  43. #include "msm_drv.h"
  44. #include "msm_kms.h"
  45. #include "msm_mmu.h"
  46. #include "sde_wb.h"
  47. #include "sde_dbg.h"
  48. /*
  49. * MSM driver version:
  50. * - 1.0.0 - initial interface
  51. * - 1.1.0 - adds madvise, and support for submits with > 4 cmd buffers
  52. * - 1.2.0 - adds explicit fence support for submit ioctl
  53. * - 1.3.0 - adds GMEM_BASE + NR_RINGS params, SUBMITQUEUE_NEW +
  54. * SUBMITQUEUE_CLOSE ioctls, and MSM_INFO_IOVA flag for
  55. * MSM_GEM_INFO ioctl.
  56. */
  57. #define MSM_VERSION_MAJOR 1
  58. #define MSM_VERSION_MINOR 3
  59. #define MSM_VERSION_PATCHLEVEL 0
  60. static void msm_fb_output_poll_changed(struct drm_device *dev)
  61. {
  62. struct msm_drm_private *priv = NULL;
  63. if (!dev) {
  64. DRM_ERROR("output_poll_changed failed, invalid input\n");
  65. return;
  66. }
  67. priv = dev->dev_private;
  68. if (priv->fbdev)
  69. drm_fb_helper_hotplug_event(priv->fbdev);
  70. }
  71. /**
  72. * msm_atomic_helper_check - validate state object
  73. * @dev: DRM device
  74. * @state: the driver state object
  75. *
  76. * This is a wrapper for the drm_atomic_helper_check to check the modeset
  77. * and state checking for planes. Additionally it checks if any secure
  78. * transition(moving CRTC and planes between secure and non-secure states and
  79. * vice versa) is allowed or not. When going to secure state, planes
  80. * with fb_mode as dir translated only can be staged on the CRTC, and only one
  81. * CRTC should be active.
  82. * Also mixing of secure and non-secure is not allowed.
  83. *
  84. * RETURNS
  85. * Zero for success or -errorno.
  86. */
  87. int msm_atomic_check(struct drm_device *dev,
  88. struct drm_atomic_state *state)
  89. {
  90. struct msm_drm_private *priv;
  91. priv = dev->dev_private;
  92. if (priv && priv->kms && priv->kms->funcs &&
  93. priv->kms->funcs->atomic_check)
  94. return priv->kms->funcs->atomic_check(priv->kms, state);
  95. return drm_atomic_helper_check(dev, state);
  96. }
  97. static const struct drm_mode_config_funcs mode_config_funcs = {
  98. .fb_create = msm_framebuffer_create,
  99. .output_poll_changed = msm_fb_output_poll_changed,
  100. .atomic_check = msm_atomic_check,
  101. .atomic_commit = msm_atomic_commit,
  102. .atomic_state_alloc = msm_atomic_state_alloc,
  103. .atomic_state_clear = msm_atomic_state_clear,
  104. .atomic_state_free = msm_atomic_state_free,
  105. };
  106. static const struct drm_mode_config_helper_funcs mode_config_helper_funcs = {
  107. .atomic_commit_tail = msm_atomic_commit_tail,
  108. };
  109. #ifdef CONFIG_DRM_MSM_REGISTER_LOGGING
  110. static bool reglog = false;
  111. MODULE_PARM_DESC(reglog, "Enable register read/write logging");
  112. module_param(reglog, bool, 0600);
  113. #else
  114. #define reglog 0
  115. #endif
  116. #ifdef CONFIG_DRM_FBDEV_EMULATION
  117. static bool fbdev = true;
  118. MODULE_PARM_DESC(fbdev, "Enable fbdev compat layer");
  119. module_param(fbdev, bool, 0600);
  120. #endif
  121. static char *vram = "16m";
  122. MODULE_PARM_DESC(vram, "Configure VRAM size (for devices without IOMMU/GPUMMU)");
  123. module_param(vram, charp, 0);
  124. bool dumpstate = false;
  125. MODULE_PARM_DESC(dumpstate, "Dump KMS state on errors");
  126. module_param(dumpstate, bool, 0600);
  127. static bool modeset = true;
  128. MODULE_PARM_DESC(modeset, "Use kernel modesetting [KMS] (1=on (default), 0=disable)");
  129. module_param(modeset, bool, 0600);
  130. /*
  131. * Util/helpers:
  132. */
  133. int msm_clk_bulk_get(struct device *dev, struct clk_bulk_data **bulk)
  134. {
  135. struct property *prop;
  136. const char *name;
  137. struct clk_bulk_data *local;
  138. int i = 0, ret, count;
  139. count = of_property_count_strings(dev->of_node, "clock-names");
  140. if (count < 1)
  141. return 0;
  142. local = devm_kcalloc(dev, sizeof(struct clk_bulk_data *),
  143. count, GFP_KERNEL);
  144. if (!local)
  145. return -ENOMEM;
  146. of_property_for_each_string(dev->of_node, "clock-names", prop, name) {
  147. local[i].id = devm_kstrdup(dev, name, GFP_KERNEL);
  148. if (!local[i].id) {
  149. devm_kfree(dev, local);
  150. return -ENOMEM;
  151. }
  152. i++;
  153. }
  154. ret = devm_clk_bulk_get(dev, count, local);
  155. if (ret) {
  156. for (i = 0; i < count; i++)
  157. devm_kfree(dev, (void *) local[i].id);
  158. devm_kfree(dev, local);
  159. return ret;
  160. }
  161. *bulk = local;
  162. return count;
  163. }
  164. struct clk *msm_clk_bulk_get_clock(struct clk_bulk_data *bulk, int count,
  165. const char *name)
  166. {
  167. int i;
  168. char n[32];
  169. snprintf(n, sizeof(n), "%s_clk", name);
  170. for (i = 0; bulk && i < count; i++) {
  171. if (!strcmp(bulk[i].id, name) || !strcmp(bulk[i].id, n))
  172. return bulk[i].clk;
  173. }
  174. return NULL;
  175. }
  176. struct clk *msm_clk_get(struct platform_device *pdev, const char *name)
  177. {
  178. struct clk *clk;
  179. char name2[32];
  180. clk = devm_clk_get(&pdev->dev, name);
  181. if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
  182. return clk;
  183. snprintf(name2, sizeof(name2), "%s_clk", name);
  184. clk = devm_clk_get(&pdev->dev, name2);
  185. if (!IS_ERR(clk))
  186. dev_warn(&pdev->dev, "Using legacy clk name binding. Use "
  187. "\"%s\" instead of \"%s\"\n", name, name2);
  188. return clk;
  189. }
  190. void __iomem *msm_ioremap(struct platform_device *pdev, const char *name,
  191. const char *dbgname)
  192. {
  193. struct resource *res;
  194. unsigned long size;
  195. void __iomem *ptr;
  196. if (name)
  197. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
  198. else
  199. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  200. if (!res) {
  201. dev_dbg(&pdev->dev, "failed to get memory resource: %s\n",
  202. name);
  203. return ERR_PTR(-EINVAL);
  204. }
  205. size = resource_size(res);
  206. ptr = devm_ioremap_nocache(&pdev->dev, res->start, size);
  207. if (!ptr) {
  208. dev_err(&pdev->dev, "failed to ioremap: %s\n", name);
  209. return ERR_PTR(-ENOMEM);
  210. }
  211. if (reglog)
  212. dev_dbg(&pdev->dev, "IO:region %s %pK %08lx\n",
  213. dbgname, ptr, size);
  214. return ptr;
  215. }
  216. unsigned long msm_iomap_size(struct platform_device *pdev, const char *name)
  217. {
  218. struct resource *res;
  219. if (name)
  220. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
  221. else
  222. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  223. if (!res) {
  224. dev_dbg(&pdev->dev, "failed to get memory resource: %s\n",
  225. name);
  226. return 0;
  227. }
  228. return resource_size(res);
  229. }
  230. void msm_iounmap(struct platform_device *pdev, void __iomem *addr)
  231. {
  232. devm_iounmap(&pdev->dev, addr);
  233. }
  234. void msm_writel(u32 data, void __iomem *addr)
  235. {
  236. if (reglog)
  237. pr_debug("IO:W %pK %08x\n", addr, data);
  238. writel(data, addr);
  239. }
  240. u32 msm_readl(const void __iomem *addr)
  241. {
  242. u32 val = readl(addr);
  243. if (reglog)
  244. pr_err("IO:R %pK %08x\n", addr, val);
  245. return val;
  246. }
  247. struct vblank_work {
  248. struct kthread_work work;
  249. int crtc_id;
  250. bool enable;
  251. struct msm_drm_private *priv;
  252. };
  253. static void vblank_ctrl_worker(struct kthread_work *work)
  254. {
  255. struct vblank_work *cur_work = container_of(work,
  256. struct vblank_work, work);
  257. struct msm_drm_private *priv = cur_work->priv;
  258. struct msm_kms *kms = priv->kms;
  259. if (cur_work->enable)
  260. kms->funcs->enable_vblank(kms, priv->crtcs[cur_work->crtc_id]);
  261. else
  262. kms->funcs->disable_vblank(kms, priv->crtcs[cur_work->crtc_id]);
  263. kfree(cur_work);
  264. }
  265. static int vblank_ctrl_queue_work(struct msm_drm_private *priv,
  266. int crtc_id, bool enable)
  267. {
  268. struct vblank_work *cur_work;
  269. if (!priv || crtc_id >= priv->num_crtcs)
  270. return -EINVAL;
  271. cur_work = kzalloc(sizeof(*cur_work), GFP_ATOMIC);
  272. if (!cur_work)
  273. return -ENOMEM;
  274. kthread_init_work(&cur_work->work, vblank_ctrl_worker);
  275. cur_work->crtc_id = crtc_id;
  276. cur_work->enable = enable;
  277. cur_work->priv = priv;
  278. kthread_queue_work(&priv->disp_thread[crtc_id].worker, &cur_work->work);
  279. return 0;
  280. }
  281. static int msm_drm_uninit(struct device *dev)
  282. {
  283. struct platform_device *pdev = to_platform_device(dev);
  284. struct drm_device *ddev = platform_get_drvdata(pdev);
  285. struct msm_drm_private *priv = ddev->dev_private;
  286. struct msm_kms *kms = priv->kms;
  287. int i;
  288. /* clean up display commit/event worker threads */
  289. for (i = 0; i < priv->num_crtcs; i++) {
  290. if (priv->disp_thread[i].thread) {
  291. kthread_flush_worker(&priv->disp_thread[i].worker);
  292. kthread_stop(priv->disp_thread[i].thread);
  293. priv->disp_thread[i].thread = NULL;
  294. }
  295. if (priv->event_thread[i].thread) {
  296. kthread_flush_worker(&priv->event_thread[i].worker);
  297. kthread_stop(priv->event_thread[i].thread);
  298. priv->event_thread[i].thread = NULL;
  299. }
  300. }
  301. drm_kms_helper_poll_fini(ddev);
  302. drm_mode_config_cleanup(ddev);
  303. if (priv->registered) {
  304. drm_dev_unregister(ddev);
  305. priv->registered = false;
  306. }
  307. #ifdef CONFIG_DRM_FBDEV_EMULATION
  308. if (fbdev && priv->fbdev)
  309. msm_fbdev_free(ddev);
  310. #endif
  311. drm_mode_config_cleanup(ddev);
  312. pm_runtime_get_sync(dev);
  313. drm_irq_uninstall(ddev);
  314. pm_runtime_put_sync(dev);
  315. flush_workqueue(priv->wq);
  316. destroy_workqueue(priv->wq);
  317. if (kms && kms->funcs)
  318. kms->funcs->destroy(kms);
  319. if (priv->vram.paddr) {
  320. unsigned long attrs = DMA_ATTR_NO_KERNEL_MAPPING;
  321. drm_mm_takedown(&priv->vram.mm);
  322. dma_free_attrs(dev, priv->vram.size, NULL,
  323. priv->vram.paddr, attrs);
  324. }
  325. component_unbind_all(dev, ddev);
  326. sde_dbg_destroy();
  327. debugfs_remove_recursive(priv->debug_root);
  328. sde_power_resource_deinit(pdev, &priv->phandle);
  329. msm_mdss_destroy(ddev);
  330. ddev->dev_private = NULL;
  331. kfree(priv);
  332. drm_dev_put(ddev);
  333. return 0;
  334. }
  335. #define KMS_MDP4 4
  336. #define KMS_MDP5 5
  337. #define KMS_SDE 3
  338. static int get_mdp_ver(struct platform_device *pdev)
  339. {
  340. #ifdef CONFIG_OF
  341. static const struct of_device_id match_types[] = { {
  342. .compatible = "qcom,mdss_mdp",
  343. .data = (void *)KMS_MDP5,
  344. },
  345. {
  346. .compatible = "qcom,sde-kms",
  347. .data = (void *)KMS_SDE,
  348. },
  349. {},
  350. };
  351. struct device *dev = &pdev->dev;
  352. const struct of_device_id *match;
  353. match = of_match_node(match_types, dev->of_node);
  354. if (match)
  355. return (int)(unsigned long)match->data;
  356. #endif
  357. return KMS_MDP4;
  358. }
  359. static int msm_init_vram(struct drm_device *dev)
  360. {
  361. struct msm_drm_private *priv = dev->dev_private;
  362. struct device_node *node;
  363. unsigned long size = 0;
  364. int ret = 0;
  365. /* In the device-tree world, we could have a 'memory-region'
  366. * phandle, which gives us a link to our "vram". Allocating
  367. * is all nicely abstracted behind the dma api, but we need
  368. * to know the entire size to allocate it all in one go. There
  369. * are two cases:
  370. * 1) device with no IOMMU, in which case we need exclusive
  371. * access to a VRAM carveout big enough for all gpu
  372. * buffers
  373. * 2) device with IOMMU, but where the bootloader puts up
  374. * a splash screen. In this case, the VRAM carveout
  375. * need only be large enough for fbdev fb. But we need
  376. * exclusive access to the buffer to avoid the kernel
  377. * using those pages for other purposes (which appears
  378. * as corruption on screen before we have a chance to
  379. * load and do initial modeset)
  380. */
  381. node = of_parse_phandle(dev->dev->of_node, "memory-region", 0);
  382. if (node) {
  383. struct resource r;
  384. ret = of_address_to_resource(node, 0, &r);
  385. of_node_put(node);
  386. if (ret)
  387. return ret;
  388. size = r.end - r.start;
  389. DRM_INFO("using VRAM carveout: %lx@%pa\n", size, &r.start);
  390. /* if we have no IOMMU, then we need to use carveout allocator.
  391. * Grab the entire CMA chunk carved out in early startup in
  392. * mach-msm:
  393. */
  394. } else if (!iommu_present(&platform_bus_type)) {
  395. DRM_INFO("using %s VRAM carveout\n", vram);
  396. size = memparse(vram, NULL);
  397. }
  398. if (size) {
  399. unsigned long attrs = 0;
  400. void *p;
  401. priv->vram.size = size;
  402. drm_mm_init(&priv->vram.mm, 0, (size >> PAGE_SHIFT) - 1);
  403. spin_lock_init(&priv->vram.lock);
  404. attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
  405. attrs |= DMA_ATTR_WRITE_COMBINE;
  406. /* note that for no-kernel-mapping, the vaddr returned
  407. * is bogus, but non-null if allocation succeeded:
  408. */
  409. p = dma_alloc_attrs(dev->dev, size,
  410. &priv->vram.paddr, GFP_KERNEL, attrs);
  411. if (!p) {
  412. dev_err(dev->dev, "failed to allocate VRAM\n");
  413. priv->vram.paddr = 0;
  414. return -ENOMEM;
  415. }
  416. dev_info(dev->dev, "VRAM: %08x->%08x\n",
  417. (uint32_t)priv->vram.paddr,
  418. (uint32_t)(priv->vram.paddr + size));
  419. }
  420. return ret;
  421. }
  422. #ifdef CONFIG_OF
  423. static int msm_component_bind_all(struct device *dev,
  424. struct drm_device *drm_dev)
  425. {
  426. int ret;
  427. ret = component_bind_all(dev, drm_dev);
  428. if (ret)
  429. DRM_ERROR("component_bind_all failed: %d\n", ret);
  430. return ret;
  431. }
  432. #else
  433. static int msm_component_bind_all(struct device *dev,
  434. struct drm_device *drm_dev)
  435. {
  436. return 0;
  437. }
  438. #endif
  439. static int msm_drm_display_thread_create(struct sched_param param,
  440. struct msm_drm_private *priv, struct drm_device *ddev,
  441. struct device *dev)
  442. {
  443. int i, ret = 0;
  444. /**
  445. * this priority was found during empiric testing to have appropriate
  446. * realtime scheduling to process display updates and interact with
  447. * other real time and normal priority task
  448. */
  449. param.sched_priority = 16;
  450. for (i = 0; i < priv->num_crtcs; i++) {
  451. /* initialize display thread */
  452. priv->disp_thread[i].crtc_id = priv->crtcs[i]->base.id;
  453. kthread_init_worker(&priv->disp_thread[i].worker);
  454. priv->disp_thread[i].dev = ddev;
  455. priv->disp_thread[i].thread =
  456. kthread_run(kthread_worker_fn,
  457. &priv->disp_thread[i].worker,
  458. "crtc_commit:%d", priv->disp_thread[i].crtc_id);
  459. ret = sched_setscheduler(priv->disp_thread[i].thread,
  460. SCHED_FIFO, &param);
  461. if (ret)
  462. pr_warn("display thread priority update failed: %d\n",
  463. ret);
  464. if (IS_ERR(priv->disp_thread[i].thread)) {
  465. dev_err(dev, "failed to create crtc_commit kthread\n");
  466. priv->disp_thread[i].thread = NULL;
  467. }
  468. /* initialize event thread */
  469. priv->event_thread[i].crtc_id = priv->crtcs[i]->base.id;
  470. kthread_init_worker(&priv->event_thread[i].worker);
  471. priv->event_thread[i].dev = ddev;
  472. priv->event_thread[i].thread =
  473. kthread_run(kthread_worker_fn,
  474. &priv->event_thread[i].worker,
  475. "crtc_event:%d", priv->event_thread[i].crtc_id);
  476. /**
  477. * event thread should also run at same priority as disp_thread
  478. * because it is handling frame_done events. A lower priority
  479. * event thread and higher priority disp_thread can causes
  480. * frame_pending counters beyond 2. This can lead to commit
  481. * failure at crtc commit level.
  482. */
  483. ret = sched_setscheduler(priv->event_thread[i].thread,
  484. SCHED_FIFO, &param);
  485. if (ret)
  486. pr_warn("display event thread priority update failed: %d\n",
  487. ret);
  488. if (IS_ERR(priv->event_thread[i].thread)) {
  489. dev_err(dev, "failed to create crtc_event kthread\n");
  490. priv->event_thread[i].thread = NULL;
  491. }
  492. if ((!priv->disp_thread[i].thread) ||
  493. !priv->event_thread[i].thread) {
  494. /* clean up previously created threads if any */
  495. for ( ; i >= 0; i--) {
  496. if (priv->disp_thread[i].thread) {
  497. kthread_stop(
  498. priv->disp_thread[i].thread);
  499. priv->disp_thread[i].thread = NULL;
  500. }
  501. if (priv->event_thread[i].thread) {
  502. kthread_stop(
  503. priv->event_thread[i].thread);
  504. priv->event_thread[i].thread = NULL;
  505. }
  506. }
  507. return -EINVAL;
  508. }
  509. }
  510. /**
  511. * Since pp interrupt is heavy weight, try to queue the work
  512. * into a dedicated worker thread, so that they dont interrupt
  513. * other important events.
  514. */
  515. kthread_init_worker(&priv->pp_event_worker);
  516. priv->pp_event_thread = kthread_run(kthread_worker_fn,
  517. &priv->pp_event_worker, "pp_event");
  518. ret = sched_setscheduler(priv->pp_event_thread,
  519. SCHED_FIFO, &param);
  520. if (ret)
  521. pr_warn("pp_event thread priority update failed: %d\n",
  522. ret);
  523. if (IS_ERR(priv->pp_event_thread)) {
  524. dev_err(dev, "failed to create pp_event kthread\n");
  525. ret = PTR_ERR(priv->pp_event_thread);
  526. priv->pp_event_thread = NULL;
  527. return ret;
  528. }
  529. return 0;
  530. }
  531. static struct msm_kms *_msm_drm_init_helper(struct msm_drm_private *priv,
  532. struct drm_device *ddev, struct device *dev,
  533. struct platform_device *pdev)
  534. {
  535. int ret;
  536. struct msm_kms *kms;
  537. switch (get_mdp_ver(pdev)) {
  538. case KMS_MDP4:
  539. kms = mdp4_kms_init(ddev);
  540. break;
  541. case KMS_MDP5:
  542. kms = mdp5_kms_init(ddev);
  543. break;
  544. case KMS_SDE:
  545. kms = sde_kms_init(ddev);
  546. break;
  547. default:
  548. kms = ERR_PTR(-ENODEV);
  549. break;
  550. }
  551. if (IS_ERR_OR_NULL(kms)) {
  552. /*
  553. * NOTE: once we have GPU support, having no kms should not
  554. * be considered fatal.. ideally we would still support gpu
  555. * and (for example) use dmabuf/prime to share buffers with
  556. * imx drm driver on iMX5
  557. */
  558. dev_err(dev, "failed to load kms\n");
  559. return kms;
  560. }
  561. priv->kms = kms;
  562. pm_runtime_enable(dev);
  563. /**
  564. * Since kms->funcs->hw_init(kms) might call
  565. * drm_object_property_set_value to initialize some custom
  566. * properties we need to make sure mode_config.funcs are populated
  567. * beforehand to avoid dereferencing an unset value during the
  568. * drm_drv_uses_atomic_modeset check.
  569. */
  570. ddev->mode_config.funcs = &mode_config_funcs;
  571. ret = (kms)->funcs->hw_init(kms);
  572. if (ret) {
  573. dev_err(dev, "kms hw init failed: %d\n", ret);
  574. return ERR_PTR(ret);
  575. }
  576. return kms;
  577. }
  578. static int msm_drm_init(struct device *dev, struct drm_driver *drv)
  579. {
  580. struct platform_device *pdev = to_platform_device(dev);
  581. struct drm_device *ddev;
  582. struct msm_drm_private *priv;
  583. struct msm_kms *kms = NULL;
  584. int ret;
  585. struct sched_param param = { 0 };
  586. ddev = drm_dev_alloc(drv, dev);
  587. if (!ddev) {
  588. dev_err(dev, "failed to allocate drm_device\n");
  589. return -ENOMEM;
  590. }
  591. drm_mode_config_init(ddev);
  592. platform_set_drvdata(pdev, ddev);
  593. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  594. if (!priv) {
  595. ret = -ENOMEM;
  596. goto priv_alloc_fail;
  597. }
  598. ddev->dev_private = priv;
  599. priv->dev = ddev;
  600. ret = msm_mdss_init(ddev);
  601. if (ret)
  602. goto mdss_init_fail;
  603. priv->wq = alloc_ordered_workqueue("msm_drm", 0);
  604. init_waitqueue_head(&priv->pending_crtcs_event);
  605. INIT_LIST_HEAD(&priv->client_event_list);
  606. INIT_LIST_HEAD(&priv->inactive_list);
  607. ret = sde_power_resource_init(pdev, &priv->phandle);
  608. if (ret) {
  609. pr_err("sde power resource init failed\n");
  610. goto power_init_fail;
  611. }
  612. ret = sde_dbg_init(&pdev->dev);
  613. if (ret) {
  614. dev_err(dev, "failed to init sde dbg: %d\n", ret);
  615. goto dbg_init_fail;
  616. }
  617. /* Bind all our sub-components: */
  618. ret = msm_component_bind_all(dev, ddev);
  619. if (ret)
  620. goto bind_fail;
  621. ret = msm_init_vram(ddev);
  622. if (ret)
  623. goto fail;
  624. ddev->mode_config.funcs = &mode_config_funcs;
  625. ddev->mode_config.helper_private = &mode_config_helper_funcs;
  626. kms = _msm_drm_init_helper(priv, ddev, dev, pdev);
  627. if (IS_ERR_OR_NULL(kms)) {
  628. dev_err(dev, "msm_drm_init_helper failed\n");
  629. goto fail;
  630. }
  631. ret = msm_drm_display_thread_create(param, priv, ddev, dev);
  632. if (ret) {
  633. dev_err(dev, "msm_drm_display_thread_create failed\n");
  634. goto fail;
  635. }
  636. ret = drm_vblank_init(ddev, priv->num_crtcs);
  637. if (ret < 0) {
  638. dev_err(dev, "failed to initialize vblank\n");
  639. goto fail;
  640. }
  641. if (kms) {
  642. pm_runtime_get_sync(dev);
  643. ret = drm_irq_install(ddev, platform_get_irq(pdev, 0));
  644. pm_runtime_put_sync(dev);
  645. if (ret < 0) {
  646. dev_err(dev, "failed to install IRQ handler\n");
  647. goto fail;
  648. }
  649. }
  650. ret = drm_dev_register(ddev, 0);
  651. if (ret)
  652. goto fail;
  653. priv->registered = true;
  654. drm_mode_config_reset(ddev);
  655. if (kms && kms->funcs && kms->funcs->cont_splash_config) {
  656. ret = kms->funcs->cont_splash_config(kms);
  657. if (ret) {
  658. dev_err(dev, "kms cont_splash config failed.\n");
  659. goto fail;
  660. }
  661. }
  662. #ifdef CONFIG_DRM_FBDEV_EMULATION
  663. if (fbdev)
  664. priv->fbdev = msm_fbdev_init(ddev);
  665. #endif
  666. priv->debug_root = debugfs_create_dir("debug",
  667. ddev->primary->debugfs_root);
  668. if (IS_ERR_OR_NULL(priv->debug_root)) {
  669. pr_err("debugfs_root create_dir fail, error %ld\n",
  670. PTR_ERR(priv->debug_root));
  671. priv->debug_root = NULL;
  672. goto fail;
  673. }
  674. ret = sde_dbg_debugfs_register(priv->debug_root);
  675. if (ret) {
  676. dev_err(dev, "failed to reg sde dbg debugfs: %d\n", ret);
  677. goto fail;
  678. }
  679. /* perform subdriver post initialization */
  680. if (kms && kms->funcs && kms->funcs->postinit) {
  681. ret = kms->funcs->postinit(kms);
  682. if (ret) {
  683. pr_err("kms post init failed: %d\n", ret);
  684. goto fail;
  685. }
  686. }
  687. drm_kms_helper_poll_init(ddev);
  688. return 0;
  689. fail:
  690. msm_drm_uninit(dev);
  691. return ret;
  692. bind_fail:
  693. sde_dbg_destroy();
  694. dbg_init_fail:
  695. sde_power_resource_deinit(pdev, &priv->phandle);
  696. power_init_fail:
  697. msm_mdss_destroy(ddev);
  698. mdss_init_fail:
  699. kfree(priv);
  700. priv_alloc_fail:
  701. drm_dev_put(ddev);
  702. return ret;
  703. }
  704. /*
  705. * DRM operations:
  706. */
  707. static int context_init(struct drm_device *dev, struct drm_file *file)
  708. {
  709. struct msm_file_private *ctx;
  710. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  711. if (!ctx)
  712. return -ENOMEM;
  713. mutex_init(&ctx->power_lock);
  714. file->driver_priv = ctx;
  715. if (dev && dev->dev_private) {
  716. struct msm_drm_private *priv = dev->dev_private;
  717. struct msm_kms *kms;
  718. kms = priv->kms;
  719. if (kms && kms->funcs && kms->funcs->postopen)
  720. kms->funcs->postopen(kms, file);
  721. }
  722. return 0;
  723. }
  724. static int msm_open(struct drm_device *dev, struct drm_file *file)
  725. {
  726. return context_init(dev, file);
  727. }
  728. static void context_close(struct msm_file_private *ctx)
  729. {
  730. kfree(ctx);
  731. }
  732. static void msm_preclose(struct drm_device *dev, struct drm_file *file)
  733. {
  734. struct msm_drm_private *priv = dev->dev_private;
  735. struct msm_kms *kms = priv->kms;
  736. if (kms && kms->funcs && kms->funcs->preclose)
  737. kms->funcs->preclose(kms, file);
  738. }
  739. static void msm_postclose(struct drm_device *dev, struct drm_file *file)
  740. {
  741. struct msm_drm_private *priv = dev->dev_private;
  742. struct msm_file_private *ctx = file->driver_priv;
  743. struct msm_kms *kms = priv->kms;
  744. if (kms && kms->funcs && kms->funcs->postclose)
  745. kms->funcs->postclose(kms, file);
  746. mutex_lock(&dev->struct_mutex);
  747. if (ctx == priv->lastctx)
  748. priv->lastctx = NULL;
  749. mutex_unlock(&dev->struct_mutex);
  750. mutex_lock(&ctx->power_lock);
  751. if (ctx->enable_refcnt) {
  752. SDE_EVT32(ctx->enable_refcnt);
  753. pm_runtime_put_sync(dev->dev);
  754. }
  755. mutex_unlock(&ctx->power_lock);
  756. context_close(ctx);
  757. }
  758. static int msm_disable_all_modes_commit(
  759. struct drm_device *dev,
  760. struct drm_atomic_state *state)
  761. {
  762. struct drm_plane *plane;
  763. struct drm_crtc *crtc;
  764. unsigned int plane_mask;
  765. int ret;
  766. plane_mask = 0;
  767. drm_for_each_plane(plane, dev) {
  768. struct drm_plane_state *plane_state;
  769. plane_state = drm_atomic_get_plane_state(state, plane);
  770. if (IS_ERR(plane_state)) {
  771. ret = PTR_ERR(plane_state);
  772. goto fail;
  773. }
  774. plane_state->rotation = 0;
  775. plane->old_fb = plane->fb;
  776. plane_mask |= 1 << drm_plane_index(plane);
  777. /* disable non-primary: */
  778. if (plane->type == DRM_PLANE_TYPE_PRIMARY)
  779. continue;
  780. DRM_DEBUG("disabling plane %d\n", plane->base.id);
  781. ret = __drm_atomic_helper_disable_plane(plane, plane_state);
  782. if (ret != 0)
  783. DRM_ERROR("error %d disabling plane %d\n", ret,
  784. plane->base.id);
  785. }
  786. drm_for_each_crtc(crtc, dev) {
  787. struct drm_mode_set mode_set;
  788. memset(&mode_set, 0, sizeof(struct drm_mode_set));
  789. mode_set.crtc = crtc;
  790. DRM_DEBUG("disabling crtc %d\n", crtc->base.id);
  791. ret = __drm_atomic_helper_set_config(&mode_set, state);
  792. if (ret != 0)
  793. DRM_ERROR("error %d disabling crtc %d\n", ret,
  794. crtc->base.id);
  795. }
  796. DRM_DEBUG("committing disables\n");
  797. ret = drm_atomic_commit(state);
  798. fail:
  799. DRM_DEBUG("disables result %d\n", ret);
  800. return ret;
  801. }
  802. /**
  803. * msm_clear_all_modes - disables all planes and crtcs via an atomic commit
  804. * based on restore_fbdev_mode_atomic in drm_fb_helper.c
  805. * @dev: device pointer
  806. * @Return: 0 on success, otherwise -error
  807. */
  808. static int msm_disable_all_modes(
  809. struct drm_device *dev,
  810. struct drm_modeset_acquire_ctx *ctx)
  811. {
  812. struct drm_atomic_state *state;
  813. int ret, i;
  814. state = drm_atomic_state_alloc(dev);
  815. if (!state)
  816. return -ENOMEM;
  817. state->acquire_ctx = ctx;
  818. for (i = 0; i < TEARDOWN_DEADLOCK_RETRY_MAX; i++) {
  819. ret = msm_disable_all_modes_commit(dev, state);
  820. if (ret != -EDEADLK || ret != -ERESTARTSYS)
  821. break;
  822. drm_atomic_state_clear(state);
  823. drm_modeset_backoff(ctx);
  824. }
  825. drm_atomic_state_put(state);
  826. return ret;
  827. }
  828. static void msm_lastclose(struct drm_device *dev)
  829. {
  830. struct msm_drm_private *priv = dev->dev_private;
  831. struct msm_kms *kms = priv->kms;
  832. struct drm_modeset_acquire_ctx ctx;
  833. int i, rc;
  834. /* check for splash status before triggering cleanup
  835. * if we end up here with splash status ON i.e before first
  836. * commit then ignore the last close call
  837. */
  838. if (kms && kms->funcs && kms->funcs->check_for_splash
  839. && kms->funcs->check_for_splash(kms))
  840. return;
  841. /*
  842. * clean up vblank disable immediately as this is the last close.
  843. */
  844. for (i = 0; i < dev->num_crtcs; i++) {
  845. struct drm_vblank_crtc *vblank = &dev->vblank[i];
  846. struct timer_list *disable_timer = &vblank->disable_timer;
  847. if (del_timer_sync(disable_timer))
  848. disable_timer->function(disable_timer);
  849. }
  850. /* wait for pending vblank requests to be executed by worker thread */
  851. flush_workqueue(priv->wq);
  852. if (priv->fbdev) {
  853. drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev);
  854. return;
  855. }
  856. drm_modeset_acquire_init(&ctx, 0);
  857. retry:
  858. rc = drm_modeset_lock_all_ctx(dev, &ctx);
  859. if (rc)
  860. goto fail;
  861. rc = msm_disable_all_modes(dev, &ctx);
  862. if (rc)
  863. goto fail;
  864. if (kms && kms->funcs && kms->funcs->lastclose)
  865. kms->funcs->lastclose(kms, &ctx);
  866. fail:
  867. if (rc == -EDEADLK) {
  868. drm_modeset_backoff(&ctx);
  869. goto retry;
  870. } else if (rc) {
  871. pr_err("last close failed: %d\n", rc);
  872. }
  873. drm_modeset_drop_locks(&ctx);
  874. drm_modeset_acquire_fini(&ctx);
  875. }
  876. static irqreturn_t msm_irq(int irq, void *arg)
  877. {
  878. struct drm_device *dev = arg;
  879. struct msm_drm_private *priv = dev->dev_private;
  880. struct msm_kms *kms = priv->kms;
  881. BUG_ON(!kms);
  882. return kms->funcs->irq(kms);
  883. }
  884. static void msm_irq_preinstall(struct drm_device *dev)
  885. {
  886. struct msm_drm_private *priv = dev->dev_private;
  887. struct msm_kms *kms = priv->kms;
  888. BUG_ON(!kms);
  889. kms->funcs->irq_preinstall(kms);
  890. }
  891. static int msm_irq_postinstall(struct drm_device *dev)
  892. {
  893. struct msm_drm_private *priv = dev->dev_private;
  894. struct msm_kms *kms = priv->kms;
  895. BUG_ON(!kms);
  896. return kms->funcs->irq_postinstall(kms);
  897. }
  898. static void msm_irq_uninstall(struct drm_device *dev)
  899. {
  900. struct msm_drm_private *priv = dev->dev_private;
  901. struct msm_kms *kms = priv->kms;
  902. BUG_ON(!kms);
  903. kms->funcs->irq_uninstall(kms);
  904. }
  905. static int msm_enable_vblank(struct drm_device *dev, unsigned int pipe)
  906. {
  907. struct msm_drm_private *priv = dev->dev_private;
  908. struct msm_kms *kms = priv->kms;
  909. if (!kms)
  910. return -ENXIO;
  911. DBG("dev=%pK, crtc=%u", dev, pipe);
  912. return vblank_ctrl_queue_work(priv, pipe, true);
  913. }
  914. static void msm_disable_vblank(struct drm_device *dev, unsigned int pipe)
  915. {
  916. struct msm_drm_private *priv = dev->dev_private;
  917. struct msm_kms *kms = priv->kms;
  918. if (!kms)
  919. return;
  920. DBG("dev=%pK, crtc=%u", dev, pipe);
  921. vblank_ctrl_queue_work(priv, pipe, false);
  922. }
  923. /*
  924. * DRM ioctls:
  925. */
  926. static int msm_ioctl_gem_new(struct drm_device *dev, void *data,
  927. struct drm_file *file)
  928. {
  929. struct drm_msm_gem_new *args = data;
  930. if (args->flags & ~MSM_BO_FLAGS) {
  931. DRM_ERROR("invalid flags: %08x\n", args->flags);
  932. return -EINVAL;
  933. }
  934. return msm_gem_new_handle(dev, file, args->size,
  935. args->flags, &args->handle);
  936. }
  937. static inline ktime_t to_ktime(struct drm_msm_timespec timeout)
  938. {
  939. return ktime_set(timeout.tv_sec, timeout.tv_nsec);
  940. }
  941. static int msm_ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
  942. struct drm_file *file)
  943. {
  944. struct drm_msm_gem_cpu_prep *args = data;
  945. struct drm_gem_object *obj;
  946. ktime_t timeout = to_ktime(args->timeout);
  947. int ret;
  948. if (args->op & ~MSM_PREP_FLAGS) {
  949. DRM_ERROR("invalid op: %08x\n", args->op);
  950. return -EINVAL;
  951. }
  952. obj = drm_gem_object_lookup(file, args->handle);
  953. if (!obj)
  954. return -ENOENT;
  955. ret = msm_gem_cpu_prep(obj, args->op, &timeout);
  956. drm_gem_object_put_unlocked(obj);
  957. return ret;
  958. }
  959. static int msm_ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
  960. struct drm_file *file)
  961. {
  962. struct drm_msm_gem_cpu_fini *args = data;
  963. struct drm_gem_object *obj;
  964. int ret;
  965. obj = drm_gem_object_lookup(file, args->handle);
  966. if (!obj)
  967. return -ENOENT;
  968. ret = msm_gem_cpu_fini(obj);
  969. drm_gem_object_put_unlocked(obj);
  970. return ret;
  971. }
  972. static int msm_ioctl_gem_madvise(struct drm_device *dev, void *data,
  973. struct drm_file *file)
  974. {
  975. struct drm_msm_gem_madvise *args = data;
  976. struct drm_gem_object *obj;
  977. int ret;
  978. switch (args->madv) {
  979. case MSM_MADV_DONTNEED:
  980. case MSM_MADV_WILLNEED:
  981. break;
  982. default:
  983. return -EINVAL;
  984. }
  985. ret = mutex_lock_interruptible(&dev->struct_mutex);
  986. if (ret)
  987. return ret;
  988. obj = drm_gem_object_lookup(file, args->handle);
  989. if (!obj) {
  990. ret = -ENOENT;
  991. goto unlock;
  992. }
  993. ret = msm_gem_madvise(obj, args->madv);
  994. if (ret >= 0) {
  995. args->retained = ret;
  996. ret = 0;
  997. }
  998. drm_gem_object_put(obj);
  999. unlock:
  1000. mutex_unlock(&dev->struct_mutex);
  1001. return ret;
  1002. }
  1003. static int msm_drm_object_supports_event(struct drm_device *dev,
  1004. struct drm_msm_event_req *req)
  1005. {
  1006. int ret = -EINVAL;
  1007. struct drm_mode_object *arg_obj;
  1008. arg_obj = drm_mode_object_find(dev, NULL, req->object_id,
  1009. req->object_type);
  1010. if (!arg_obj)
  1011. return -ENOENT;
  1012. switch (arg_obj->type) {
  1013. case DRM_MODE_OBJECT_CRTC:
  1014. case DRM_MODE_OBJECT_CONNECTOR:
  1015. ret = 0;
  1016. break;
  1017. default:
  1018. ret = -EOPNOTSUPP;
  1019. break;
  1020. }
  1021. drm_mode_object_put(arg_obj);
  1022. return ret;
  1023. }
  1024. static int msm_register_event(struct drm_device *dev,
  1025. struct drm_msm_event_req *req, struct drm_file *file, bool en)
  1026. {
  1027. int ret = -EINVAL;
  1028. struct msm_drm_private *priv = dev->dev_private;
  1029. struct msm_kms *kms = priv->kms;
  1030. struct drm_mode_object *arg_obj;
  1031. arg_obj = drm_mode_object_find(dev, file, req->object_id,
  1032. req->object_type);
  1033. if (!arg_obj)
  1034. return -ENOENT;
  1035. ret = kms->funcs->register_events(kms, arg_obj, req->event, en);
  1036. drm_mode_object_put(arg_obj);
  1037. return ret;
  1038. }
  1039. static int msm_event_client_count(struct drm_device *dev,
  1040. struct drm_msm_event_req *req_event, bool locked)
  1041. {
  1042. struct msm_drm_private *priv = dev->dev_private;
  1043. unsigned long flag = 0;
  1044. struct msm_drm_event *node;
  1045. int count = 0;
  1046. if (!locked)
  1047. spin_lock_irqsave(&dev->event_lock, flag);
  1048. list_for_each_entry(node, &priv->client_event_list, base.link) {
  1049. if (node->event.type == req_event->event &&
  1050. node->info.object_id == req_event->object_id)
  1051. count++;
  1052. }
  1053. if (!locked)
  1054. spin_unlock_irqrestore(&dev->event_lock, flag);
  1055. return count;
  1056. }
  1057. static int msm_ioctl_register_event(struct drm_device *dev, void *data,
  1058. struct drm_file *file)
  1059. {
  1060. struct msm_drm_private *priv = dev->dev_private;
  1061. struct drm_msm_event_req *req_event = data;
  1062. struct msm_drm_event *client, *node;
  1063. unsigned long flag = 0;
  1064. bool dup_request = false;
  1065. int ret = 0, count = 0;
  1066. ret = msm_drm_object_supports_event(dev, req_event);
  1067. if (ret) {
  1068. DRM_ERROR("unsupported event %x object %x object id %d\n",
  1069. req_event->event, req_event->object_type,
  1070. req_event->object_id);
  1071. return ret;
  1072. }
  1073. spin_lock_irqsave(&dev->event_lock, flag);
  1074. list_for_each_entry(node, &priv->client_event_list, base.link) {
  1075. if (node->base.file_priv != file)
  1076. continue;
  1077. if (node->event.type == req_event->event &&
  1078. node->info.object_id == req_event->object_id) {
  1079. DRM_DEBUG("duplicate request for event %x obj id %d\n",
  1080. node->event.type, node->info.object_id);
  1081. dup_request = true;
  1082. break;
  1083. }
  1084. }
  1085. spin_unlock_irqrestore(&dev->event_lock, flag);
  1086. if (dup_request)
  1087. return -EALREADY;
  1088. client = kzalloc(sizeof(*client), GFP_KERNEL);
  1089. if (!client)
  1090. return -ENOMEM;
  1091. client->base.file_priv = file;
  1092. client->base.event = &client->event;
  1093. client->event.type = req_event->event;
  1094. memcpy(&client->info, req_event, sizeof(client->info));
  1095. /* Get the count of clients that have registered for event.
  1096. * Event should be enabled for first client, for subsequent enable
  1097. * calls add to client list and return.
  1098. */
  1099. count = msm_event_client_count(dev, req_event, false);
  1100. /* Add current client to list */
  1101. spin_lock_irqsave(&dev->event_lock, flag);
  1102. list_add_tail(&client->base.link, &priv->client_event_list);
  1103. spin_unlock_irqrestore(&dev->event_lock, flag);
  1104. if (count)
  1105. return 0;
  1106. ret = msm_register_event(dev, req_event, file, true);
  1107. if (ret) {
  1108. DRM_ERROR("failed to enable event %x object %x object id %d\n",
  1109. req_event->event, req_event->object_type,
  1110. req_event->object_id);
  1111. spin_lock_irqsave(&dev->event_lock, flag);
  1112. list_del(&client->base.link);
  1113. spin_unlock_irqrestore(&dev->event_lock, flag);
  1114. kfree(client);
  1115. }
  1116. return ret;
  1117. }
  1118. static int msm_ioctl_deregister_event(struct drm_device *dev, void *data,
  1119. struct drm_file *file)
  1120. {
  1121. struct msm_drm_private *priv = dev->dev_private;
  1122. struct drm_msm_event_req *req_event = data;
  1123. struct msm_drm_event *client = NULL, *node, *temp;
  1124. unsigned long flag = 0;
  1125. int count = 0;
  1126. bool found = false;
  1127. int ret = 0;
  1128. ret = msm_drm_object_supports_event(dev, req_event);
  1129. if (ret) {
  1130. DRM_ERROR("unsupported event %x object %x object id %d\n",
  1131. req_event->event, req_event->object_type,
  1132. req_event->object_id);
  1133. return ret;
  1134. }
  1135. spin_lock_irqsave(&dev->event_lock, flag);
  1136. list_for_each_entry_safe(node, temp, &priv->client_event_list,
  1137. base.link) {
  1138. if (node->event.type == req_event->event &&
  1139. node->info.object_id == req_event->object_id &&
  1140. node->base.file_priv == file) {
  1141. client = node;
  1142. list_del(&client->base.link);
  1143. found = true;
  1144. kfree(client);
  1145. break;
  1146. }
  1147. }
  1148. spin_unlock_irqrestore(&dev->event_lock, flag);
  1149. if (!found)
  1150. return -ENOENT;
  1151. count = msm_event_client_count(dev, req_event, false);
  1152. if (!count)
  1153. ret = msm_register_event(dev, req_event, file, false);
  1154. return ret;
  1155. }
  1156. void msm_mode_object_event_notify(struct drm_mode_object *obj,
  1157. struct drm_device *dev, struct drm_event *event, u8 *payload)
  1158. {
  1159. struct msm_drm_private *priv = NULL;
  1160. unsigned long flags;
  1161. struct msm_drm_event *notify, *node;
  1162. int len = 0, ret;
  1163. if (!obj || !event || !event->length || !payload) {
  1164. DRM_ERROR("err param obj %pK event %pK len %d payload %pK\n",
  1165. obj, event, ((event) ? (event->length) : -1),
  1166. payload);
  1167. return;
  1168. }
  1169. priv = (dev) ? dev->dev_private : NULL;
  1170. if (!dev || !priv) {
  1171. DRM_ERROR("invalid dev %pK priv %pK\n", dev, priv);
  1172. return;
  1173. }
  1174. spin_lock_irqsave(&dev->event_lock, flags);
  1175. list_for_each_entry(node, &priv->client_event_list, base.link) {
  1176. if (node->event.type != event->type ||
  1177. obj->id != node->info.object_id)
  1178. continue;
  1179. len = event->length + sizeof(struct msm_drm_event);
  1180. if (node->base.file_priv->event_space < len) {
  1181. DRM_ERROR("Insufficient space %d for event %x len %d\n",
  1182. node->base.file_priv->event_space, event->type,
  1183. len);
  1184. continue;
  1185. }
  1186. notify = kzalloc(len, GFP_ATOMIC);
  1187. if (!notify)
  1188. continue;
  1189. notify->base.file_priv = node->base.file_priv;
  1190. notify->base.event = &notify->event;
  1191. notify->event.type = node->event.type;
  1192. notify->event.length = event->length +
  1193. sizeof(struct drm_msm_event_resp);
  1194. memcpy(&notify->info, &node->info, sizeof(notify->info));
  1195. memcpy(notify->data, payload, event->length);
  1196. ret = drm_event_reserve_init_locked(dev, node->base.file_priv,
  1197. &notify->base, &notify->event);
  1198. if (ret) {
  1199. kfree(notify);
  1200. continue;
  1201. }
  1202. drm_send_event_locked(dev, &notify->base);
  1203. }
  1204. spin_unlock_irqrestore(&dev->event_lock, flags);
  1205. }
  1206. static int msm_release(struct inode *inode, struct file *filp)
  1207. {
  1208. struct drm_file *file_priv = filp->private_data;
  1209. struct drm_minor *minor = file_priv->minor;
  1210. struct drm_device *dev = minor->dev;
  1211. struct msm_drm_private *priv = dev->dev_private;
  1212. struct msm_drm_event *node, *temp, *tmp_node;
  1213. u32 count;
  1214. unsigned long flags;
  1215. LIST_HEAD(tmp_head);
  1216. spin_lock_irqsave(&dev->event_lock, flags);
  1217. list_for_each_entry_safe(node, temp, &priv->client_event_list,
  1218. base.link) {
  1219. if (node->base.file_priv != file_priv)
  1220. continue;
  1221. list_del(&node->base.link);
  1222. list_add_tail(&node->base.link, &tmp_head);
  1223. }
  1224. spin_unlock_irqrestore(&dev->event_lock, flags);
  1225. list_for_each_entry_safe(node, temp, &tmp_head,
  1226. base.link) {
  1227. list_del(&node->base.link);
  1228. count = msm_event_client_count(dev, &node->info, false);
  1229. list_for_each_entry(tmp_node, &tmp_head, base.link) {
  1230. if (tmp_node->event.type == node->info.event &&
  1231. tmp_node->info.object_id ==
  1232. node->info.object_id)
  1233. count++;
  1234. }
  1235. if (!count)
  1236. msm_register_event(dev, &node->info, file_priv, false);
  1237. kfree(node);
  1238. }
  1239. return drm_release(inode, filp);
  1240. }
  1241. /**
  1242. * msm_ioctl_rmfb2 - remove an FB from the configuration
  1243. * @dev: drm device for the ioctl
  1244. * @data: data pointer for the ioctl
  1245. * @file_priv: drm file for the ioctl call
  1246. *
  1247. * Remove the FB specified by the user.
  1248. *
  1249. * Called by the user via ioctl.
  1250. *
  1251. * Returns:
  1252. * Zero on success, negative errno on failure.
  1253. */
  1254. int msm_ioctl_rmfb2(struct drm_device *dev, void *data,
  1255. struct drm_file *file_priv)
  1256. {
  1257. struct drm_framebuffer *fb = NULL;
  1258. struct drm_framebuffer *fbl = NULL;
  1259. uint32_t *id = data;
  1260. int found = 0;
  1261. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1262. return -EINVAL;
  1263. fb = drm_framebuffer_lookup(dev, file_priv, *id);
  1264. if (!fb)
  1265. return -ENOENT;
  1266. /* drop extra ref from traversing drm_framebuffer_lookup */
  1267. drm_framebuffer_put(fb);
  1268. mutex_lock(&file_priv->fbs_lock);
  1269. list_for_each_entry(fbl, &file_priv->fbs, filp_head)
  1270. if (fb == fbl)
  1271. found = 1;
  1272. if (!found) {
  1273. mutex_unlock(&file_priv->fbs_lock);
  1274. return -ENOENT;
  1275. }
  1276. list_del_init(&fb->filp_head);
  1277. mutex_unlock(&file_priv->fbs_lock);
  1278. drm_framebuffer_put(fb);
  1279. return 0;
  1280. }
  1281. EXPORT_SYMBOL(msm_ioctl_rmfb2);
  1282. /**
  1283. * msm_ioctl_power_ctrl - enable/disable power vote on MDSS Hw
  1284. * @dev: drm device for the ioctl
  1285. * @data: data pointer for the ioctl
  1286. * @file_priv: drm file for the ioctl call
  1287. *
  1288. */
  1289. int msm_ioctl_power_ctrl(struct drm_device *dev, void *data,
  1290. struct drm_file *file_priv)
  1291. {
  1292. struct msm_file_private *ctx = file_priv->driver_priv;
  1293. struct msm_drm_private *priv;
  1294. struct drm_msm_power_ctrl *power_ctrl = data;
  1295. bool vote_req = false;
  1296. int old_cnt;
  1297. int rc = 0;
  1298. if (unlikely(!power_ctrl)) {
  1299. DRM_ERROR("invalid ioctl data\n");
  1300. return -EINVAL;
  1301. }
  1302. priv = dev->dev_private;
  1303. mutex_lock(&ctx->power_lock);
  1304. old_cnt = ctx->enable_refcnt;
  1305. if (power_ctrl->enable) {
  1306. if (!ctx->enable_refcnt)
  1307. vote_req = true;
  1308. ctx->enable_refcnt++;
  1309. } else if (ctx->enable_refcnt) {
  1310. ctx->enable_refcnt--;
  1311. if (!ctx->enable_refcnt)
  1312. vote_req = true;
  1313. } else {
  1314. pr_err("ignoring, unbalanced disable\n");
  1315. }
  1316. if (vote_req) {
  1317. if (power_ctrl->enable)
  1318. rc = pm_runtime_get_sync(dev->dev);
  1319. else
  1320. pm_runtime_put_sync(dev->dev);
  1321. if (rc < 0)
  1322. ctx->enable_refcnt = old_cnt;
  1323. }
  1324. pr_debug("pid %d enable %d, refcnt %d, vote_req %d\n",
  1325. current->pid, power_ctrl->enable, ctx->enable_refcnt,
  1326. vote_req);
  1327. SDE_EVT32(current->pid, power_ctrl->enable, ctx->enable_refcnt,
  1328. vote_req);
  1329. mutex_unlock(&ctx->power_lock);
  1330. return rc;
  1331. }
  1332. static const struct drm_ioctl_desc msm_ioctls[] = {
  1333. DRM_IOCTL_DEF_DRV(MSM_GEM_NEW, msm_ioctl_gem_new, DRM_AUTH|DRM_RENDER_ALLOW),
  1334. DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_PREP, msm_ioctl_gem_cpu_prep, DRM_AUTH|DRM_RENDER_ALLOW),
  1335. DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_FINI, msm_ioctl_gem_cpu_fini, DRM_AUTH|DRM_RENDER_ALLOW),
  1336. DRM_IOCTL_DEF_DRV(MSM_GEM_MADVISE, msm_ioctl_gem_madvise, DRM_AUTH|DRM_RENDER_ALLOW),
  1337. DRM_IOCTL_DEF_DRV(SDE_WB_CONFIG, sde_wb_config, DRM_UNLOCKED|DRM_AUTH),
  1338. DRM_IOCTL_DEF_DRV(MSM_REGISTER_EVENT, msm_ioctl_register_event,
  1339. DRM_UNLOCKED),
  1340. DRM_IOCTL_DEF_DRV(MSM_DEREGISTER_EVENT, msm_ioctl_deregister_event,
  1341. DRM_UNLOCKED),
  1342. DRM_IOCTL_DEF_DRV(MSM_RMFB2, msm_ioctl_rmfb2, DRM_UNLOCKED),
  1343. DRM_IOCTL_DEF_DRV(MSM_POWER_CTRL, msm_ioctl_power_ctrl,
  1344. DRM_RENDER_ALLOW),
  1345. };
  1346. static const struct vm_operations_struct vm_ops = {
  1347. .fault = msm_gem_fault,
  1348. .open = drm_gem_vm_open,
  1349. .close = drm_gem_vm_close,
  1350. };
  1351. static const struct file_operations fops = {
  1352. .owner = THIS_MODULE,
  1353. .open = drm_open,
  1354. .release = msm_release,
  1355. .unlocked_ioctl = drm_ioctl,
  1356. .compat_ioctl = drm_compat_ioctl,
  1357. .poll = drm_poll,
  1358. .read = drm_read,
  1359. .llseek = no_llseek,
  1360. .mmap = msm_gem_mmap,
  1361. };
  1362. static struct drm_driver msm_driver = {
  1363. .driver_features = DRIVER_HAVE_IRQ |
  1364. DRIVER_GEM |
  1365. DRIVER_PRIME |
  1366. DRIVER_RENDER |
  1367. DRIVER_ATOMIC |
  1368. DRIVER_MODESET,
  1369. .open = msm_open,
  1370. .preclose = msm_preclose,
  1371. .postclose = msm_postclose,
  1372. .lastclose = msm_lastclose,
  1373. .irq_handler = msm_irq,
  1374. .irq_preinstall = msm_irq_preinstall,
  1375. .irq_postinstall = msm_irq_postinstall,
  1376. .irq_uninstall = msm_irq_uninstall,
  1377. .enable_vblank = msm_enable_vblank,
  1378. .disable_vblank = msm_disable_vblank,
  1379. .gem_free_object = msm_gem_free_object,
  1380. .gem_vm_ops = &vm_ops,
  1381. .dumb_create = msm_gem_dumb_create,
  1382. .dumb_map_offset = msm_gem_dumb_map_offset,
  1383. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  1384. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  1385. .gem_prime_export = drm_gem_prime_export,
  1386. .gem_prime_import = msm_gem_prime_import,
  1387. .gem_prime_res_obj = msm_gem_prime_res_obj,
  1388. .gem_prime_pin = msm_gem_prime_pin,
  1389. .gem_prime_unpin = msm_gem_prime_unpin,
  1390. .gem_prime_get_sg_table = msm_gem_prime_get_sg_table,
  1391. .gem_prime_import_sg_table = msm_gem_prime_import_sg_table,
  1392. .gem_prime_vmap = msm_gem_prime_vmap,
  1393. .gem_prime_vunmap = msm_gem_prime_vunmap,
  1394. .gem_prime_mmap = msm_gem_prime_mmap,
  1395. .ioctls = msm_ioctls,
  1396. .num_ioctls = ARRAY_SIZE(msm_ioctls),
  1397. .fops = &fops,
  1398. .name = "msm_drm",
  1399. .desc = "MSM Snapdragon DRM",
  1400. .date = "20130625",
  1401. .major = MSM_VERSION_MAJOR,
  1402. .minor = MSM_VERSION_MINOR,
  1403. .patchlevel = MSM_VERSION_PATCHLEVEL,
  1404. };
  1405. #ifdef CONFIG_PM_SLEEP
  1406. static int msm_pm_suspend(struct device *dev)
  1407. {
  1408. struct drm_device *ddev;
  1409. struct msm_drm_private *priv;
  1410. struct msm_kms *kms;
  1411. if (!dev)
  1412. return -EINVAL;
  1413. ddev = dev_get_drvdata(dev);
  1414. if (!ddev || !ddev->dev_private)
  1415. return -EINVAL;
  1416. priv = ddev->dev_private;
  1417. kms = priv->kms;
  1418. if (kms && kms->funcs && kms->funcs->pm_suspend)
  1419. return kms->funcs->pm_suspend(dev);
  1420. /* disable hot-plug polling */
  1421. drm_kms_helper_poll_disable(ddev);
  1422. return 0;
  1423. }
  1424. static int msm_pm_resume(struct device *dev)
  1425. {
  1426. struct drm_device *ddev;
  1427. struct msm_drm_private *priv;
  1428. struct msm_kms *kms;
  1429. if (!dev)
  1430. return -EINVAL;
  1431. ddev = dev_get_drvdata(dev);
  1432. if (!ddev || !ddev->dev_private)
  1433. return -EINVAL;
  1434. priv = ddev->dev_private;
  1435. kms = priv->kms;
  1436. if (kms && kms->funcs && kms->funcs->pm_resume)
  1437. return kms->funcs->pm_resume(dev);
  1438. /* enable hot-plug polling */
  1439. drm_kms_helper_poll_enable(ddev);
  1440. return 0;
  1441. }
  1442. #endif
  1443. #ifdef CONFIG_PM
  1444. static int msm_runtime_suspend(struct device *dev)
  1445. {
  1446. struct drm_device *ddev = dev_get_drvdata(dev);
  1447. struct msm_drm_private *priv = ddev->dev_private;
  1448. DBG("");
  1449. if (priv->mdss)
  1450. msm_mdss_disable(priv->mdss);
  1451. else
  1452. sde_power_resource_enable(&priv->phandle, false);
  1453. return 0;
  1454. }
  1455. static int msm_runtime_resume(struct device *dev)
  1456. {
  1457. struct drm_device *ddev = dev_get_drvdata(dev);
  1458. struct msm_drm_private *priv = ddev->dev_private;
  1459. int ret;
  1460. DBG("");
  1461. if (priv->mdss)
  1462. ret = msm_mdss_enable(priv->mdss);
  1463. else
  1464. ret = sde_power_resource_enable(&priv->phandle, true);
  1465. return ret;
  1466. }
  1467. #endif
  1468. static const struct dev_pm_ops msm_pm_ops = {
  1469. SET_SYSTEM_SLEEP_PM_OPS(msm_pm_suspend, msm_pm_resume)
  1470. SET_RUNTIME_PM_OPS(msm_runtime_suspend, msm_runtime_resume, NULL)
  1471. };
  1472. /*
  1473. * Componentized driver support:
  1474. */
  1475. /*
  1476. * NOTE: duplication of the same code as exynos or imx (or probably any other).
  1477. * so probably some room for some helpers
  1478. */
  1479. static int compare_of(struct device *dev, void *data)
  1480. {
  1481. return dev->of_node == data;
  1482. }
  1483. /*
  1484. * Identify what components need to be added by parsing what remote-endpoints
  1485. * our MDP output ports are connected to. In the case of LVDS on MDP4, there
  1486. * is no external component that we need to add since LVDS is within MDP4
  1487. * itself.
  1488. */
  1489. static int add_components_mdp(struct device *mdp_dev,
  1490. struct component_match **matchptr)
  1491. {
  1492. struct device_node *np = mdp_dev->of_node;
  1493. struct device_node *ep_node;
  1494. struct device *master_dev;
  1495. /*
  1496. * on MDP4 based platforms, the MDP platform device is the component
  1497. * master that adds other display interface components to itself.
  1498. *
  1499. * on MDP5 based platforms, the MDSS platform device is the component
  1500. * master that adds MDP5 and other display interface components to
  1501. * itself.
  1502. */
  1503. if (of_device_is_compatible(np, "qcom,mdp4"))
  1504. master_dev = mdp_dev;
  1505. else
  1506. master_dev = mdp_dev->parent;
  1507. for_each_endpoint_of_node(np, ep_node) {
  1508. struct device_node *intf;
  1509. struct of_endpoint ep;
  1510. int ret;
  1511. ret = of_graph_parse_endpoint(ep_node, &ep);
  1512. if (ret) {
  1513. dev_err(mdp_dev, "unable to parse port endpoint\n");
  1514. of_node_put(ep_node);
  1515. return ret;
  1516. }
  1517. /*
  1518. * The LCDC/LVDS port on MDP4 is a speacial case where the
  1519. * remote-endpoint isn't a component that we need to add
  1520. */
  1521. if (of_device_is_compatible(np, "qcom,mdp4") &&
  1522. ep.port == 0)
  1523. continue;
  1524. /*
  1525. * It's okay if some of the ports don't have a remote endpoint
  1526. * specified. It just means that the port isn't connected to
  1527. * any external interface.
  1528. */
  1529. intf = of_graph_get_remote_port_parent(ep_node);
  1530. if (!intf)
  1531. continue;
  1532. drm_of_component_match_add(master_dev, matchptr, compare_of,
  1533. intf);
  1534. of_node_put(intf);
  1535. }
  1536. return 0;
  1537. }
  1538. static int compare_name_mdp(struct device *dev, void *data)
  1539. {
  1540. return (strnstr(dev_name(dev), "mdp", strlen("mdp")) != NULL);
  1541. }
  1542. static int add_display_components(struct device *dev,
  1543. struct component_match **matchptr)
  1544. {
  1545. struct device *mdp_dev = NULL;
  1546. struct device_node *node;
  1547. int ret;
  1548. if (of_device_is_compatible(dev->of_node, "qcom,sde-kms")) {
  1549. struct device_node *np = dev->of_node;
  1550. unsigned int i;
  1551. for (i = 0; ; i++) {
  1552. node = of_parse_phandle(np, "connectors", i);
  1553. if (!node)
  1554. break;
  1555. component_match_add(dev, matchptr, compare_of, node);
  1556. }
  1557. return 0;
  1558. }
  1559. /*
  1560. * MDP5 based devices don't have a flat hierarchy. There is a top level
  1561. * parent: MDSS, and children: MDP5, DSI, HDMI, eDP etc. Populate the
  1562. * children devices, find the MDP5 node, and then add the interfaces
  1563. * to our components list.
  1564. */
  1565. if (of_device_is_compatible(dev->of_node, "qcom,mdss")) {
  1566. ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
  1567. if (ret) {
  1568. dev_err(dev, "failed to populate children devices\n");
  1569. return ret;
  1570. }
  1571. mdp_dev = device_find_child(dev, NULL, compare_name_mdp);
  1572. if (!mdp_dev) {
  1573. dev_err(dev, "failed to find MDSS MDP node\n");
  1574. of_platform_depopulate(dev);
  1575. return -ENODEV;
  1576. }
  1577. put_device(mdp_dev);
  1578. /* add the MDP component itself */
  1579. component_match_add(dev, matchptr, compare_of,
  1580. mdp_dev->of_node);
  1581. } else {
  1582. /* MDP4 */
  1583. mdp_dev = dev;
  1584. }
  1585. ret = add_components_mdp(mdp_dev, matchptr);
  1586. if (ret)
  1587. of_platform_depopulate(dev);
  1588. return ret;
  1589. }
  1590. struct msm_gem_address_space *
  1591. msm_gem_smmu_address_space_get(struct drm_device *dev,
  1592. unsigned int domain)
  1593. {
  1594. struct msm_drm_private *priv = NULL;
  1595. struct msm_kms *kms;
  1596. const struct msm_kms_funcs *funcs;
  1597. if ((!dev) || (!dev->dev_private))
  1598. return NULL;
  1599. priv = dev->dev_private;
  1600. kms = priv->kms;
  1601. if (!kms)
  1602. return NULL;
  1603. funcs = kms->funcs;
  1604. if ((!funcs) || (!funcs->get_address_space))
  1605. return NULL;
  1606. return funcs->get_address_space(priv->kms, domain);
  1607. }
  1608. static int msm_drm_bind(struct device *dev)
  1609. {
  1610. return msm_drm_init(dev, &msm_driver);
  1611. }
  1612. static void msm_drm_unbind(struct device *dev)
  1613. {
  1614. msm_drm_uninit(dev);
  1615. }
  1616. static const struct component_master_ops msm_drm_ops = {
  1617. .bind = msm_drm_bind,
  1618. .unbind = msm_drm_unbind,
  1619. };
  1620. /*
  1621. * Platform driver:
  1622. */
  1623. static int msm_pdev_probe(struct platform_device *pdev)
  1624. {
  1625. int ret;
  1626. struct component_match *match = NULL;
  1627. ret = add_display_components(&pdev->dev, &match);
  1628. if (ret)
  1629. return ret;
  1630. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  1631. return component_master_add_with_match(&pdev->dev, &msm_drm_ops, match);
  1632. }
  1633. static int msm_pdev_remove(struct platform_device *pdev)
  1634. {
  1635. component_master_del(&pdev->dev, &msm_drm_ops);
  1636. of_platform_depopulate(&pdev->dev);
  1637. msm_drm_unbind(&pdev->dev);
  1638. component_master_del(&pdev->dev, &msm_drm_ops);
  1639. return 0;
  1640. }
  1641. static void msm_pdev_shutdown(struct platform_device *pdev)
  1642. {
  1643. struct drm_device *ddev = platform_get_drvdata(pdev);
  1644. struct msm_drm_private *priv = NULL;
  1645. if (!ddev) {
  1646. DRM_ERROR("invalid drm device node\n");
  1647. return;
  1648. }
  1649. priv = ddev->dev_private;
  1650. if (!priv) {
  1651. DRM_ERROR("invalid msm drm private node\n");
  1652. return;
  1653. }
  1654. msm_lastclose(ddev);
  1655. /* set this after lastclose to allow kickoff from lastclose */
  1656. priv->shutdown_in_progress = true;
  1657. }
  1658. static const struct of_device_id dt_match[] = {
  1659. { .compatible = "qcom,mdp4", .data = (void *)KMS_MDP4 },
  1660. { .compatible = "qcom,mdss", .data = (void *)KMS_MDP5 },
  1661. { .compatible = "qcom,sde-kms", .data = (void *)KMS_SDE },
  1662. {},
  1663. };
  1664. MODULE_DEVICE_TABLE(of, dt_match);
  1665. static struct platform_driver msm_platform_driver = {
  1666. .probe = msm_pdev_probe,
  1667. .remove = msm_pdev_remove,
  1668. .shutdown = msm_pdev_shutdown,
  1669. .driver = {
  1670. .name = "msm_drm",
  1671. .of_match_table = dt_match,
  1672. .pm = &msm_pm_ops,
  1673. .suppress_bind_attrs = true,
  1674. },
  1675. };
  1676. #ifdef CONFIG_QCOM_KGSL
  1677. void __init adreno_register(void)
  1678. {
  1679. }
  1680. void __exit adreno_unregister(void)
  1681. {
  1682. }
  1683. #endif
  1684. static int __init msm_drm_register(void)
  1685. {
  1686. if (!modeset)
  1687. return -EINVAL;
  1688. DBG("init");
  1689. msm_smmu_driver_init();
  1690. msm_dsi_register();
  1691. msm_edp_register();
  1692. msm_hdmi_register();
  1693. adreno_register();
  1694. return platform_driver_register(&msm_platform_driver);
  1695. }
  1696. static void __exit msm_drm_unregister(void)
  1697. {
  1698. DBG("fini");
  1699. platform_driver_unregister(&msm_platform_driver);
  1700. msm_hdmi_unregister();
  1701. adreno_unregister();
  1702. msm_edp_unregister();
  1703. msm_dsi_unregister();
  1704. msm_smmu_driver_cleanup();
  1705. }
  1706. module_init(msm_drm_register);
  1707. module_exit(msm_drm_unregister);
  1708. MODULE_AUTHOR("Rob Clark <[email protected]");
  1709. MODULE_DESCRIPTION("MSM DRM Driver");
  1710. MODULE_LICENSE("GPL");