msm_drv.c 51 KB

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