msm_drv.c 53 KB

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